Skip to content

Commit 98ba6ee

Browse files
author
robot
committed
feat: upgrade to v3
1 parent ec35b6d commit 98ba6ee

File tree

7 files changed

+313
-14
lines changed

7 files changed

+313
-14
lines changed

leetcode-cheat.zip

47.6 KB
Binary file not shown.

public/manifest.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"manifest_version": 2,
2+
"manifest_version": 3,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.11.5",
6-
"browser_action": {
5+
"version": "0.12.0",
6+
"action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"
99
},
@@ -24,10 +24,17 @@
2424
}
2525
],
2626
"web_accessible_resources": [
27-
"/static/js/content.js"
27+
{
28+
"resources": [
29+
"/static/js/content.js"
30+
]
31+
}
2832
],
2933
"permissions": [
3034
"tabs"
3135
],
32-
"content_security_policy": "script-src 'self' 'sha256-9HcBuUP35aPkU0991A4mASdsuifTkUlifJ7elThz6Ow=' 'sha256-0Jo/EYaXS11i7poc/P9fGcq/o6P0djny2JW6WivTVVw='; object-src 'self'"
36+
"content_security_policy": {
37+
"extension_pages": "script-src 'self' 'sha256-9HcBuUP35aPkU0991A4mASdsuifTkUlifJ7elThz6Ow=' 'sha256-0Jo/EYaXS11i7poc/P9fGcq/o6P0djny2JW6WivTVVw='; object-src 'self'",
38+
"sandbox": "script-src 'self' 'sha256-9HcBuUP35aPkU0991A4mASdsuifTkUlifJ7elThz6Ow=' 'sha256-0Jo/EYaXS11i7poc/P9fGcq/o6P0djny2JW6WivTVVw='; object-src 'self'"
39+
}
3340
}

src/codeTemplates/segmentTree.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
import segmentLogo from "../imgs/segment.svg";
22
import { t } from "../locales";
33

4-
const pyCode = `
4+
const binaryIndexTreePyCode = `
5+
class BinaryIndexTree:
6+
__slots__ = 'nums', 'tree'
7+
8+
def __init__(self, nums: List[int]):
9+
n = len(nums)
10+
self.nums = [0] * n
11+
self.tree = [0] * (n + 1) # n + 1 只是方便计算前缀和,前面加了一个 0,所以总长度就是 n + 1
12+
for i, x in enumerate(nums):
13+
self.update(i, x)
14+
15+
def update(self, index: int, val: int) -> None:
16+
delta = val - self.nums[index]
17+
self.nums[index] = val
18+
i = index + 1 # + 1 的原因同上,也是前面多加了一个 0,导致所有索引都偏移了一位
19+
while i < len(self.tree):
20+
self.tree[i] += delta
21+
i += i & -i
22+
23+
def prefixSum(self, i: int) -> int:
24+
s = 0
25+
while i:
26+
s += self.tree[i]
27+
i -= i & -i
28+
return s
29+
def querySum(self, l: int, r: int) -> int:
30+
if r < l: return 0
31+
return self.prefixSum(r+1) - self.prefixSum(l)
32+
`;
33+
const segmentTreePyCode = `
534
class SegmentTree:
635
def __init__(self, data:List[int]):
736
'''
@@ -133,6 +162,32 @@ export default () => ({
133162
logo: segmentLogo,
134163
link: "https://oi-wiki.org/ds/seg/",
135164
list: [
165+
{
166+
text: t("Locale.codeTemplate.segmentTree.item5"),
167+
problems: [
168+
{
169+
id: "range-sum-query-mutable",
170+
// title: "303. 区域和检索 - 数组不可变(使用一维前缀和会更简单)",
171+
title: t("Locale.problem.303"),
172+
},
173+
{
174+
id: "range-sum-query-mutable",
175+
// title: "307. 区域和检索 - 数组可修改",
176+
title: t("Locale.problem.307"),
177+
},
178+
{
179+
id: "peaks-in-array",
180+
// title: "3187. 数组中的峰值",
181+
title: t("Locale.problem.3187"),
182+
},
183+
],
184+
codes: [
185+
{
186+
language: "py",
187+
text: binaryIndexTreePyCode,
188+
},
189+
],
190+
},
136191
{
137192
// text: "区间和线段树",
138193
text: t("Locale.codeTemplate.segmentTree.item1"),
@@ -151,7 +206,7 @@ export default () => ({
151206
codes: [
152207
{
153208
language: "py",
154-
text: pyCode,
209+
text: segmentTreePyCode,
155210
},
156211
],
157212
},
@@ -358,7 +413,7 @@ class SegmentTree:
358413
],
359414
},
360415
{
361-
// text: "动态开点",
416+
// text: "动态开点线段树",
362417
text: t("Locale.codeTemplate.segmentTree.item4"),
363418
problems: [
364419
{

src/db/root.db.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13992,6 +13992,55 @@
1399213992
}
1399313993
]
1399413994
},
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+
},
1399514044
"minimum-white-tiles-after-covering-with-carpets":{
1399614045
"id": "2209",
1399714046
"name": "minimum-white-tiles-after-covering-with-carpets",
@@ -14081,6 +14130,33 @@
1408114130
}
1408214131
]
1408314132
},
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+
},
1408414160
"distribute-money-to-maximum-children":{
1408514161
"id": "2591",
1408614162
"name": "distribute-money-to-maximum-children",
@@ -14231,6 +14307,42 @@
1423114307
}
1423214308
]
1423314309
},
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+
},
1423414346
"beautiful-towers-ii":{
1423514347
"id": "2866",
1423614348
"name": "beautiful-towers-ii",
@@ -14289,6 +14401,127 @@
1428914401
}
1429014402
]
1429114403
},
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+
},
1429214525
"selling-pieces-of-wood":{
1429314526
"id": "5254",
1429414527
"name": "selling-pieces-of-wood",

0 commit comments

Comments
 (0)