|
13992 | 13992 | }
|
13993 | 13993 | ]
|
13994 | 13994 | },
|
| 13995 | +"count-good-triplets-in-an-array":{ |
| 13996 | + "id": "2172", |
| 13997 | + "name": "count-good-triplets-in-an-array", |
| 13998 | + "pre": [ |
| 13999 | + { |
| 14000 | + "text": "平衡二叉树", |
| 14001 | + "link": null, |
| 14002 | + "color": "geekblue" |
| 14003 | + }, |
| 14004 | + { |
| 14005 | + "text": "枚举", |
| 14006 | + "link": null, |
| 14007 | + "color": "magenta" |
| 14008 | + } |
| 14009 | + ], |
| 14010 | + "keyPoints": [ |
| 14011 | + { |
| 14012 | + "text": "根据数组A的索引对应关系置换数组B,得到新的数组C,问题转化为堆C求递增三元组的个数", |
| 14013 | + "link": null, |
| 14014 | + "color": "blue" |
| 14015 | + }, |
| 14016 | + { |
| 14017 | + "text": "枚举三元组中中间的数", |
| 14018 | + "link": null, |
| 14019 | + "color": "blue" |
| 14020 | + } |
| 14021 | + ], |
| 14022 | + "companies": [], |
| 14023 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2172.count-good-triplets-in-an-array.md", |
| 14024 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2172.count-good-triplets-in-an-array.md", |
| 14025 | + "code": [ |
| 14026 | + { |
| 14027 | + "language": "py", |
| 14028 | + "text": "\nn = len(nums1)\nfor i in range(n):\n d[nums1[i]] = i\n" |
| 14029 | + }, |
| 14030 | + { |
| 14031 | + "language": "py", |
| 14032 | + "text": "\nfor i in range(n):\n nums.append(d[nums2[i]])\n" |
| 14033 | + }, |
| 14034 | + { |
| 14035 | + "language": "py", |
| 14036 | + "text": "\nsl1 = SortedList()\nsl2 = SortedList(nums)\nfor num in nums:\n sl1.add(num)\n sl2.remove(num)\n ans += sl1.bisect_left(num) * (len(sl2) - sl2.bisect_left(num + 1))\nreturn ans\n" |
| 14037 | + }, |
| 14038 | + { |
| 14039 | + "language": "py", |
| 14040 | + "text": "\n\nfrom sortedcontainers import SortedList\nclass Solution:\n def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n d = {}\n nums = []\n ans = 0\n n = len(nums1)\n for i in range(n):\n d[nums1[i]] = i\n for i in range(n):\n nums.append(d[nums2[i]])\n sl1 = SortedList()\n for num in nums:\n sl1.add(num)\n ans += sl1.bisect_left(num) * ((n - num - (len(sl1) - sl1.bisect_left(num))))\n return ans\n\n" |
| 14041 | + } |
| 14042 | + ] |
| 14043 | +}, |
13995 | 14044 | "minimum-white-tiles-after-covering-with-carpets":{
|
13996 | 14045 | "id": "2209",
|
13997 | 14046 | "name": "minimum-white-tiles-after-covering-with-carpets",
|
|
14081 | 14130 | }
|
14082 | 14131 | ]
|
14083 | 14132 | },
|
| 14133 | +"selling-pieces-of-wood":{ |
| 14134 | + "id": "2312", |
| 14135 | + "name": "selling-pieces-of-wood", |
| 14136 | + "pre": [ |
| 14137 | + { |
| 14138 | + "text": "动态规划记忆化递归", |
| 14139 | + "link": null, |
| 14140 | + "color": "geekblue" |
| 14141 | + } |
| 14142 | + ], |
| 14143 | + "keyPoints": [ |
| 14144 | + { |
| 14145 | + "text": "枚举切割点", |
| 14146 | + "link": null, |
| 14147 | + "color": "blue" |
| 14148 | + } |
| 14149 | + ], |
| 14150 | + "companies": [], |
| 14151 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2312.selling-pieces-of-wood.md", |
| 14152 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2312.selling-pieces-of-wood.md", |
| 14153 | + "code": [ |
| 14154 | + { |
| 14155 | + "language": "py", |
| 14156 | + "text": "\n\nclass Solution:\n def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:\n d = {(h, w): p for h, w, p in prices}\n @cache\n def dp(i, j):\n ans = d.get((i, j), 0) # 不切\n # 竖着切\n for x in range(1, i):\n ans = max(ans, dp(x, j) + dp(i - x, j))\n # 横着切\n for y in range(1, j):\n ans = max(ans, dp(i, y) + dp(i, j - y))\n return ans # 且三种选择的最大值即可\n return dp(m, n)\n\n" |
| 14157 | + } |
| 14158 | + ] |
| 14159 | +}, |
14084 | 14160 | "distribute-money-to-maximum-children":{
|
14085 | 14161 | "id": "2591",
|
14086 | 14162 | "name": "distribute-money-to-maximum-children",
|
|
14231 | 14307 | }
|
14232 | 14308 | ]
|
14233 | 14309 | },
|
| 14310 | +"beautiful-towers-i":{ |
| 14311 | + "id": "2865", |
| 14312 | + "name": "beautiful-towers-i", |
| 14313 | + "pre": [ |
| 14314 | + { |
| 14315 | + "text": "单调栈", |
| 14316 | + "link": null, |
| 14317 | + "color": "purple" |
| 14318 | + } |
| 14319 | + ], |
| 14320 | + "keyPoints": [ |
| 14321 | + { |
| 14322 | + "text": "单调栈优化", |
| 14323 | + "link": null, |
| 14324 | + "color": "blue" |
| 14325 | + }, |
| 14326 | + { |
| 14327 | + "text": "动态规划", |
| 14328 | + "link": null, |
| 14329 | + "color": "blue" |
| 14330 | + } |
| 14331 | + ], |
| 14332 | + "companies": [], |
| 14333 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2865.beautiful-towers-i.md", |
| 14334 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2865.beautiful-towers-i.md", |
| 14335 | + "code": [ |
| 14336 | + { |
| 14337 | + "language": "py", |
| 14338 | + "text": "\nans, n = 0, len(maxHeight)\n for i, x in enumerate(maxHeight):\n y = t = x\n # t 是高度和,y 是 min_v\n for j in range(i - 1, -1, -1):\n y = min(y, maxHeight[j])\n t += y\n y = x\n for j in range(i + 1, n):\n y = min(y, maxHeight[j])\n t += y\n ans = max(ans, t)\n return ans\n" |
| 14339 | + }, |
| 14340 | + { |
| 14341 | + "language": "py", |
| 14342 | + "text": "\n\nclass Solution:\n def maximumSumOfHeights(self, maxHeight: List[int]) -> int:\n n = len(maxHeight)\n f = [-1] * n # f[i] 表示 i 作为峰顶左侧的高度和\n g = [-1] * n # g[i] 表示 -i-1 作为峰顶右侧的高度和\n def gao(f):\n st = []\n for i in range(len(maxHeight)):\n while st and maxHeight[i] <= maxHeight[st[-1]]:\n st.pop()\n if st:\n f[i] = (i - st[-1]) * maxHeight[i] + f[st[-1]]\n else:\n f[i] = maxHeight[i] * (i + 1)\n st.append(i)\n gao(f)\n maxHeight = maxHeight[::-1]\n gao(g)\n maxHeight = maxHeight[::-1]\n ans = 0\n for i in range(len(maxHeight)):\n ans = max(ans, f[i] + g[-i-1] - maxHeight[i])\n return ans\n\n" |
| 14343 | + } |
| 14344 | + ] |
| 14345 | +}, |
14234 | 14346 | "beautiful-towers-ii":{
|
14235 | 14347 | "id": "2866",
|
14236 | 14348 | "name": "beautiful-towers-ii",
|
@@ -14289,6 +14401,127 @@
|
14289 | 14401 | }
|
14290 | 14402 | ]
|
14291 | 14403 | },
|
| 14404 | +"count-the-number-of-incremovable-subarrays-ii":{ |
| 14405 | + "id": "2972", |
| 14406 | + "name": "count-the-number-of-incremovable-subarrays-ii", |
| 14407 | + "pre": [], |
| 14408 | + "keyPoints": [ |
| 14409 | + { |
| 14410 | + "text": "枚举每一个后缀对答案的贡献", |
| 14411 | + "link": null, |
| 14412 | + "color": "blue" |
| 14413 | + } |
| 14414 | + ], |
| 14415 | + "companies": [], |
| 14416 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2972.count-the-number-of-incremovable-subarrays-ii.md", |
| 14417 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2972.count-the-number-of-incremovable-subarrays-ii.md", |
| 14418 | + "code": [ |
| 14419 | + { |
| 14420 | + "language": "py", |
| 14421 | + "text": "\n\nclass Solution:\n def incremovableSubarrayCount(self, nums: List[int]) -> int:\n i = 0\n n = len(nums)\n while i < n - 1 and nums[i] < nums[i+1]:\n i += 1\n if i == n - 1: return (n * (n + 1)) // 2\n j = n - 1\n ans = i + 2 # 后缀是空的时候,答案是 i + 2\n while j > -1:\n if j+1<n and nums[j] >= nums[j+1]: break # 后缀不再递增,不满足 2\n while i > -1 and nums[j] <= nums[i]:\n i -= 1 # 只能靠缩小前缀来满足。而 i 不回退,因此时间复杂度还是 n\n j -= 1\n ans += i + 2\n return ans\n \n\n" |
| 14422 | + } |
| 14423 | + ] |
| 14424 | +}, |
| 14425 | +"find-the-number-of-ways-to-place-people-ii":{ |
| 14426 | + "id": "3027", |
| 14427 | + "name": "find-the-number-of-ways-to-place-people-ii", |
| 14428 | + "pre": [ |
| 14429 | + { |
| 14430 | + "text": "暂无", |
| 14431 | + "link": null, |
| 14432 | + "color": "green" |
| 14433 | + } |
| 14434 | + ], |
| 14435 | + "keyPoints": [ |
| 14436 | + { |
| 14437 | + "text": "排序", |
| 14438 | + "link": null, |
| 14439 | + "color": "blue" |
| 14440 | + } |
| 14441 | + ], |
| 14442 | + "companies": [], |
| 14443 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/3027.find-the-number-of-ways-to-place-people-ii.md", |
| 14444 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/3027.find-the-number-of-ways-to-place-people-ii.md", |
| 14445 | + "code": [ |
| 14446 | + { |
| 14447 | + "language": "py", |
| 14448 | + "text": "\n\nclass Solution:\n def numberOfPairs(self, points: List[List[int]]) -> int:\n points.sort(key=lambda p: (p[0], -p[1]))\n ans = 0\n for i, (x1, y1) in enumerate(points): # point i\n max_y = -inf\n min_y = inf\n for (x2, y2) in points[i + 1:]: # point j\n if y1 < y2: continue # 确保条件1\n if y2 > max_y or y1 < min_y: # 确保条件2\n ans += 1\n max_y = max(max_y, y2)\n min_y = min(min_y, y2)\n return ans\n\n" |
| 14449 | + } |
| 14450 | + ] |
| 14451 | +}, |
| 14452 | +"maximize-consecutive-elements-in-an-array-after-modification":{ |
| 14453 | + "id": "3041", |
| 14454 | + "name": "maximize-consecutive-elements-in-an-array-after-modification", |
| 14455 | + "pre": [ |
| 14456 | + { |
| 14457 | + "text": "动态规划", |
| 14458 | + "link": null, |
| 14459 | + "color": "red" |
| 14460 | + } |
| 14461 | + ], |
| 14462 | + "keyPoints": [ |
| 14463 | + { |
| 14464 | + "text": "将以每一个元素结尾的最长连续子序列的长度统统存起来", |
| 14465 | + "link": null, |
| 14466 | + "color": "blue" |
| 14467 | + } |
| 14468 | + ], |
| 14469 | + "companies": [], |
| 14470 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/3041.maximize-consecutive-elements-in-an-array-after-modification.md", |
| 14471 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/3041.maximize-consecutive-elements-in-an-array-after-modification.md", |
| 14472 | + "code": [ |
| 14473 | + { |
| 14474 | + "language": "py", |
| 14475 | + "text": "\n\nclass Solution:\n def maxSelectedElements(self, arr: List[int]) -> int:\n memo = collections.defaultdict(int)\n arr.sort()\n def dp(pos):\n if pos == len(arr): return 0\n memo[arr[pos]+1] = memo[arr[pos]]+1 # 由于可以重排,因此这一句要写\n memo[arr[pos]] = memo[arr[pos]-1]+1\n dp(pos+1)\n dp(0)\n return max(memo.values())\n\n\n" |
| 14476 | + } |
| 14477 | + ] |
| 14478 | +}, |
| 14479 | +"find-the-sum-of-the-power-of-all-subsequences":{ |
| 14480 | + "id": "3082", |
| 14481 | + "name": "find-the-sum-of-the-power-of-all-subsequences", |
| 14482 | + "pre": [ |
| 14483 | + { |
| 14484 | + "text": "动态规划", |
| 14485 | + "link": null, |
| 14486 | + "color": "red" |
| 14487 | + } |
| 14488 | + ], |
| 14489 | + "keyPoints": [ |
| 14490 | + { |
| 14491 | + "text": "分解问题", |
| 14492 | + "link": null, |
| 14493 | + "color": "blue" |
| 14494 | + } |
| 14495 | + ], |
| 14496 | + "companies": [], |
| 14497 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/3082.find-the-sum-of-the-power-of-all-subsequences.md", |
| 14498 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/3082.find-the-sum-of-the-power-of-all-subsequences.md", |
| 14499 | + "code": [ |
| 14500 | + { |
| 14501 | + "language": "py", |
| 14502 | + "text": "\ndef f(i, k):\n if i == n:\n if k == 0: 找到了\n else: 没找到\n if k == 0:\n 没找到\n f(i + 1, k) # 不选择\n f(i + 1, k - nums[i]) # 选择\n" |
| 14503 | + }, |
| 14504 | + { |
| 14505 | + "language": "py", |
| 14506 | + "text": "\n\nclass Solution:\n def sumOfPower(self, nums: List[int], k: int) -> int:\n n = len(nums)\n MOD = 10 ** 9 + 7\n @cache\n def dfs(i, k):\n if k == 0: return pow(2, n - i, MOD)\n if i == n or k < 0: return 0\n ans = dfs(i + 1, k) * 2 # 不选\n ans += dfs(i + 1, k - nums[i]) # 选\n return ans % MOD\n \n return dfs(0, k)\n\n" |
| 14507 | + } |
| 14508 | + ] |
| 14509 | +}, |
| 14510 | +"minimum-cost-walk-in-weighted-graph":{ |
| 14511 | + "id": "3108", |
| 14512 | + "name": "minimum-cost-walk-in-weighted-graph", |
| 14513 | + "pre": [], |
| 14514 | + "keyPoints": [], |
| 14515 | + "companies": [], |
| 14516 | + "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/3108.minimum-cost-walk-in-weighted-graph.md", |
| 14517 | + "solution": "https://github.com/azl397985856/leetcode/blob/master/problems/3108.minimum-cost-walk-in-weighted-graph.md", |
| 14518 | + "code": [ |
| 14519 | + { |
| 14520 | + "language": "py", |
| 14521 | + "text": "\n\n\nclass UF:\n def __init__(self, M):\n self.parent = {}\n self.cnt = 0\n self.all_and = {}\n # 初始化 parent,size 和 cnt\n # Initialize parent, size and cnt\n for i in range(M):\n self.parent[i] = i\n self.cnt += 1\n self.all_and[i] = 2 ** 30 - 1 # 也可以初始化为 -1\n\n def find(self, x):\n if x != self.parent[x]:\n self.parent[x] = self.find(self.parent[x])\n return self.parent[x]\n return x\n def union(self, p, q, w):\n # if self.connected(p, q): return # 这道题对于联通的情况不能直接 return,具体可以参考示例 2. 环的存在\n leader_p = self.find(p)\n leader_q = self.find(q)\n self.parent[leader_p] = leader_q\n # p 连通块的 and 值为 w1,q 连通块的 and 值为 w2,合并后就是 w1 & w2 & w\n self.all_and[leader_p] = self.all_and[leader_q] = self.all_and[leader_p] & w & self.all_and[leader_q]\n self.cnt -= 1\n def connected(self, p, q):\n return self.find(p) == self.find(q)\n \nclass Solution:\n def minimumCost(self, n: int, edges: List[List[int]], query: List[List[int]]) -> List[int]:\n g = [[] for _ in range(n)]\n uf = UF(n)\n for x, y, w in edges:\n g[x].append((y, w))\n g[y].append((x, w))\n uf.union(x, y, w)\n\n ans = []\n for s, t in query:\n if not uf.connected(s, t):\n ans.append(-1)\n else:\n ans.append(uf.all_and[uf.parent[s]])\n return ans\n\n\n\n\n" |
| 14522 | + } |
| 14523 | + ] |
| 14524 | +}, |
14292 | 14525 | "selling-pieces-of-wood":{
|
14293 | 14526 | "id": "5254",
|
14294 | 14527 | "name": "selling-pieces-of-wood",
|
|
0 commit comments