|
2565 | 2565 | "sort-colors":{
|
2566 | 2566 | "id": "75",
|
2567 | 2567 | "name": "sort-colors",
|
2568 |
| - "pre": [ |
2569 |
| - { |
2570 |
| - "text": "荷兰国旗问题", |
2571 |
| - "link": "https://en.wikipedia.org/wiki/Dutch_national_flag_problem", |
2572 |
| - "color": "purple" |
2573 |
| - }, |
2574 |
| - { |
2575 |
| - "text": "排序", |
2576 |
| - "link": null, |
2577 |
| - "color": "purple" |
2578 |
| - } |
2579 |
| - ], |
2580 |
| - "keyPoints": [ |
2581 |
| - { |
2582 |
| - "text": "荷兰国旗问题", |
2583 |
| - "link": null, |
2584 |
| - "color": "blue" |
2585 |
| - }, |
2586 |
| - { |
2587 |
| - "text": "countingsort", |
2588 |
| - "link": null, |
2589 |
| - "color": "blue" |
2590 |
| - } |
2591 |
| - ], |
2592 |
| - "companies": [ |
2593 |
| - { |
2594 |
| - "name": "阿里巴巴" |
2595 |
| - }, |
2596 |
| - { |
2597 |
| - "name": "腾讯" |
2598 |
| - }, |
2599 |
| - { |
2600 |
| - "name": "百度" |
2601 |
| - }, |
2602 |
| - { |
2603 |
| - "name": "字节跳动" |
2604 |
| - } |
2605 |
| - ], |
| 2568 | + "pre": [], |
| 2569 | + "keyPoints": [], |
| 2570 | + "companies": [], |
2606 | 2571 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/75.sort-colors.md",
|
2607 | 2572 | "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/75.sort-colors.md",
|
2608 |
| - "code": [ |
2609 |
| - { |
2610 |
| - "language": "cpp", |
2611 |
| - "text": "\nclass Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int r = 0, g = 0, b = 0;\n for (int n : nums) {\n if (n == 0) {\n nums[b++] = 2;\n nums[g++] = 1;\n nums[r++] = 0;\n } else if (n == 1) {\n nums[b++] = 2;\n nums[g++] = 1;\n } else nums[b++] = 2;\n }\n }\n};\n" |
2612 |
| - }, |
2613 |
| - { |
2614 |
| - "language": "py", |
2615 |
| - "text": "\nclass Solution:\n def sortColors(self, strs):\n # p0 是右边界\n # p1 是右边界\n # p2 是左边界\n # p1 超过 p2 结束\n p0, p1, p2 = 0, 0, len(strs) - 1\n\n while p1 <= p2:\n if strs[p1] == 'blue':\n strs[p2], strs[p1] = strs[p1], strs[p2]\n p2 -= 1\n elif strs[p1] == 'red':\n strs[p0], strs[p1] = strs[p1], strs[p0]\n p0 += 1\n p1 += 1 # p0 一定不是 blue,因此 p1 += 1\n else: # p1 === 'green'\n p1 += 1\n return strs\n" |
2616 |
| - }, |
2617 |
| - { |
2618 |
| - "language": "py", |
2619 |
| - "text": "\nclass Solution:\n def partition(self, head: ListNode, x: int) -> ListNode:\n l1 = cur = head\n while cur:\n if cur.val < x:\n cur.val, l1.val = l1.val, cur.val\n l1 = l1.next\n cur = cur.next\n return head\n" |
2620 |
| - } |
2621 |
| - ] |
| 2573 | + "code": [] |
2622 | 2574 | },
|
2623 | 2575 | "subsets":{
|
2624 | 2576 | "id": "78",
|
|
13426 | 13378 | }
|
13427 | 13379 | ]
|
13428 | 13380 | },
|
| 13381 | +"maximum-score-of-a-good-subarray":{ |
| 13382 | + "id": "1793", |
| 13383 | + "name": "maximum-score-of-a-good-subarray", |
| 13384 | + "pre": [ |
| 13385 | + { |
| 13386 | + "text": "单调栈", |
| 13387 | + "link": null, |
| 13388 | + "color": "purple" |
| 13389 | + } |
| 13390 | + ], |
| 13391 | + "keyPoints": [ |
| 13392 | + { |
| 13393 | + "text": "贡献法", |
| 13394 | + "link": null, |
| 13395 | + "color": "blue" |
| 13396 | + }, |
| 13397 | + { |
| 13398 | + "text": "单调栈", |
| 13399 | + "link": null, |
| 13400 | + "color": "blue" |
| 13401 | + } |
| 13402 | + ], |
| 13403 | + "companies": [], |
| 13404 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1793.maximum-score-of-a-good-subarray.md", |
| 13405 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1793.maximum-score-of-a-good-subarray.md", |
| 13406 | + "code": [ |
| 13407 | + { |
| 13408 | + "language": "py", |
| 13409 | + "text": "\nclass Solution:\n def maximumScore(self, nums: List[int], k: int) -> int:\n # 单调栈求出 nums[i] 的下一个更小的下标 j\n st = []\n ans = 0\n nums += [0]\n for i in range(len(nums)):\n while st and nums[st[-1]] > nums[i]:\n # 含义:st[-1] 的下一个更小的是 i\n left = st[-2] if len(st) > 1 else -1 # 注意这里是 -2,因为 st[-1] 是当前元素, 我们要在当前元素的左边记录找。也可以先 st.pop() 后在 st[-1]\n if left < k < i: # 注意由于 left 和 i 我们都无法取到(开区间),因此这里不能有等号\n ans = max(ans, (i - left - 1) * nums[st[-1]])\n st.pop()\n st.append(i)\n return ans\n" |
| 13410 | + } |
| 13411 | + ] |
| 13412 | +}, |
13429 | 13413 | "single-threaded-cpu":{
|
13430 | 13414 | "id": "1834",
|
13431 | 13415 | "name": "single-threaded-cpu",
|
|
14102 | 14086 | }
|
14103 | 14087 | ]
|
14104 | 14088 | },
|
| 14089 | +"minimum-absolute-difference-between-elements-with-constraint":{ |
| 14090 | + "id": "2817", |
| 14091 | + "name": "minimum-absolute-difference-between-elements-with-constraint", |
| 14092 | + "pre": [ |
| 14093 | + { |
| 14094 | + "text": "二分查找", |
| 14095 | + "link": null, |
| 14096 | + "color": "magenta" |
| 14097 | + } |
| 14098 | + ], |
| 14099 | + "keyPoints": [], |
| 14100 | + "companies": [], |
| 14101 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2817.minimum-absolute-difference-between-elements-with-constraint.md", |
| 14102 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2817.minimum-absolute-difference-between-elements-with-constraint.md", |
| 14103 | + "code": [ |
| 14104 | + { |
| 14105 | + "language": "py", |
| 14106 | + "text": "\nclass Solution:\n def minAbsoluteDifference(self, nums: List[int], x: int) -> int:\n n = len(nums)\n minDiff = float('inf')\n\n for i in range(n):\n for j in range(i + x, n):\n absDiff = abs(nums[i] - nums[j])\n if absDiff < minDiff:\n minDiff = absDiff\n\n return minDiff\n\n" |
| 14107 | + }, |
| 14108 | + { |
| 14109 | + "language": "py", |
| 14110 | + "text": "\nfrom sortedcontainers import SortedList\n\nclass Solution:\n def minAbsoluteDifference(self, nums: List[int], x: int) -> int:\n n = len(nums)\n\n # 初始化答案为无穷大\n res = float('inf') \n\n # 维护前面元素的有序序列\n ls = SortedList() \n\n for i in range(n - x):\n\n # 将nums[i]加入有序序列ls,SortedList保证插入后仍然有序\n v = nums[i]\n ls.add(v) \n\n # 使用二分查找寻找前面序列中最后一个<=nums[i+x]的元素\n v = nums[i + x]\n idx = ls.bisect_right(v)\n\n # 使用和nums[i+x]最接近的元素更新答案,将答案更新为当前答案和新差值中的较小值\n res = min(res, abs(v - ls[idx - 1]))\n\n # 如果存在更接近的元素,也尝试更新答案\n if idx < len(ls): \n res = min(res, abs(ls[idx] - v))\n\n return res\n" |
| 14111 | + } |
| 14112 | + ] |
| 14113 | +}, |
14105 | 14114 | "selling-pieces-of-wood":{
|
14106 | 14115 | "id": "5254",
|
14107 | 14116 | "name": "selling-pieces-of-wood",
|
|
0 commit comments