Skip to content

Commit 4c8c322

Browse files
author
robot
committed
feat: 0.10.5
1 parent e99c7ce commit 4c8c322

File tree

4 files changed

+173
-8
lines changed

4 files changed

+173
-8
lines changed

leetcode-cheat-0.10.3.zip

-4.26 MB
Binary file not shown.
4.26 MB
Binary file not shown.

public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.10.4",
5+
"version": "0.10.5",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

src/db/root.db.js

Lines changed: 172 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,10 +3444,6 @@
34443444
"language": "js",
34453445
"text": "\nvar isSameTree = function (p, q) {\n const preorderP = preorder(p, []);\n const preorderQ = preorder(q, []);\n const inorderP = inorder(p, []);\n const inorderQ = inorder(q, []);\n return (\n preorderP.join(\"\") === preorderQ.join(\"\") &&\n inorderP.join(\"\") === inorderQ.join(\"\")\n );\n};\n\nfunction preorder(root, arr) {\n if (root === null) {\n arr.push(\" \");\n return arr;\n }\n arr.push(root.val);\n preorder(root.left, arr);\n preorder(root.right, arr);\n return arr;\n}\n\nfunction inorder(root, arr) {\n if (root === null) {\n arr.push(\" \");\n return arr;\n }\n inorder(root.left, arr);\n arr.push(root.val);\n inorder(root.right, arr);\n return arr;\n}\n"
34463446
},
3447-
{
3448-
"language": "cpp",
3449-
"text": "\nclass Solution {\npublic:\n bool isSameTree(TreeNode* p, TreeNode* q) {\n return (!p && !q) || (p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));\n }\n};\n"
3450-
},
34513447
{
34523448
"language": "cpp",
34533449
"text": "\nclass Solution {\npublic:\n bool isSameTree(TreeNode* p, TreeNode* q) {\n return (!p && !q) || (p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));\n }\n};\n"
@@ -12801,6 +12797,50 @@
1280112797
}
1280212798
]
1280312799
},
12800+
"number-of-ways-to-form-a-target-string-given-a-dictionary":{
12801+
"id": "1639",
12802+
"name": "number-of-ways-to-form-a-target-string-given-a-dictionary",
12803+
"pre": [
12804+
{
12805+
"text": "哈希表",
12806+
"link": null,
12807+
"color": "gold"
12808+
},
12809+
{
12810+
"text": "动态规划",
12811+
"link": null,
12812+
"color": "red"
12813+
}
12814+
],
12815+
"keyPoints": [
12816+
{
12817+
"text": "使用哈希表加速dp状态转移",
12818+
"link": null,
12819+
"color": "blue"
12820+
}
12821+
],
12822+
"companies": [],
12823+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1639.number-of-ways-to-form-a-target-string-given-a-dictionary.md",
12824+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1639.number-of-ways-to-form-a-target-string-given-a-dictionary.md",
12825+
"code": [
12826+
{
12827+
"language": "py",
12828+
"text": "\nclass Solution:\n def numWays(self, words: List[str], target: str) -> int:\n MOD = 10 ** 9 + 7\n k = len(words[0])\n cnt = [[0] * k for _ in range(26)]\n for j in range(k):\n for word in words:\n cnt[ord(word[j]) - ord('a')][j] += 1\n @cache\n def dp(col, pos):\n if len(target) - pos > len(words[0]) - col: return 0 # 剪枝\n if pos == len(target): return 1\n if col == len(words[0]): return 0\n ans = dp(col+1, pos) # skip\n for word in words: # pick one of the word[col]\n if word[col] == target[pos]:\n ans += dp(col+1, pos+1)\n ans %= MOD\n return ans % MOD\n return dp(0, 0) % MOD\n"
12829+
},
12830+
{
12831+
"language": "py",
12832+
"text": "\nfor word in words: # pick one of the word[col]\n if word[col] == target[pos]:\n ans += dp(col+1, pos+1)\n ans %= MOD\n"
12833+
},
12834+
{
12835+
"language": "py",
12836+
"text": "\ncnt = [[0] * k for _ in range(26)]\nfor j in range(k):\n for word in words:\n cnt[ord(word[j]) - ord('a')][j] += 1\n"
12837+
},
12838+
{
12839+
"language": "py",
12840+
"text": "\n\nclass Solution:\n def numWays(self, words: List[str], target: str) -> int:\n MOD = 10 ** 9 + 7\n k = len(words[0])\n cnt = [[0] * k for _ in range(26)]\n for j in range(k):\n for word in words:\n cnt[ord(word[j]) - ord('a')][j] += 1\n @cache\n def dp(col, pos):\n if len(target) - pos > len(words[0]) - col: return 0 # 剪枝\n if pos == len(target): return 1\n if col == len(words[0]): return 0\n ans = dp(col+1, pos) # skip\n ans += dp(col+1, pos+1) * cnt[ord(target[pos]) - ord('a')][col] # 根据上面的提示,我们可以这样优化\n return ans % MOD\n return dp(0, 0) % MOD\n\n"
12841+
}
12842+
]
12843+
},
1280412844
"create-sorted-array-through-instructions":{
1280512845
"id": "1649",
1280612846
"name": "create-sorted-array-through-instructions",
@@ -13726,6 +13766,110 @@
1372613766
}
1372713767
]
1372813768
},
13769+
"distribute-money-to-maximum-children":{
13770+
"id": "2591",
13771+
"name": "distribute-money-to-maximum-children",
13772+
"pre": [
13773+
{
13774+
"text": "动态规划",
13775+
"link": null,
13776+
"color": "red"
13777+
},
13778+
{
13779+
"text": "脑筋急转弯",
13780+
"link": null,
13781+
"color": "cyan"
13782+
}
13783+
],
13784+
"keyPoints": [
13785+
{
13786+
"text": "先每个人分配一块钱,保证题目约束”每个人“都需要分到。",
13787+
"link": null,
13788+
"color": "blue"
13789+
},
13790+
{
13791+
"text": "贪心",
13792+
"link": null,
13793+
"color": "blue"
13794+
}
13795+
],
13796+
"companies": [],
13797+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2591.distribute-money-to-maximum-children.md",
13798+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2591.distribute-money-to-maximum-children.md",
13799+
"code": [
13800+
{
13801+
"language": "py",
13802+
"text": "\n\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n # @cache\n # def dp(money, children):\n # if children == 0:\n # if money == 0: return 0\n # return -inf\n # if money == 0: return -inf\n # ans = -inf\n # for i in range(1, money+1):\n # if i == 4: continue\n # ans = max(ans, int(i == 8) + dp(money - i, children - 1))\n # return ans\n # ans = dp(money, children)\n # if ans == -inf: return -1\n # return ans\n if money < children: return -1\n dp = [[-inf] * (children+1) for _ in range(money+1)]\n dp[0][0] = 0\n for i in range(money+1):\n for j in range(1, children+1):\n for k in range(1, i+1):\n if k == 4: continue\n dp[i][j] = max(dp[i][j], int(k == 8) + dp[i - k][j - 1])\n return -1 if dp[-1][-1] == -inf else dp[-1][-1]\n\n"
13803+
},
13804+
{
13805+
"language": "py",
13806+
"text": "\n\nclass Solution:\n def distMoney(self, money: int, children: int) -> int:\n money -= children # 每人至少 1 美元\n if money < 0: return -1\n ans = min(money // 7, children) # 初步分配,让尽量多的人分到 8 美元\n money -= ans * 7\n children -= ans\n # children == 0 and money:必须找一个前面分了 8 美元的人,分配完剩余的钱\n # children == 1 and money == 3:不能有人恰好分到 4 美元\n if children == 0 and money or \\\n children == 1 and money == 3:\n ans -= 1\n return ans\n\n"
13807+
}
13808+
]
13809+
},
13810+
"maximize-greatness-of-an-array":{
13811+
"id": "2592",
13812+
"name": "maximize-greatness-of-an-array",
13813+
"pre": [
13814+
{
13815+
"text": "二分",
13816+
"link": null,
13817+
"color": "purple"
13818+
},
13819+
{
13820+
"text": "贪心",
13821+
"link": null,
13822+
"color": "purple"
13823+
}
13824+
],
13825+
"keyPoints": [
13826+
{
13827+
"text": "能力检测二分",
13828+
"link": null,
13829+
"color": "blue"
13830+
}
13831+
],
13832+
"companies": [],
13833+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2592.maximize-greatness-of-an-array.md",
13834+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2592.maximize-greatness-of-an-array.md",
13835+
"code": [
13836+
{
13837+
"language": "py",
13838+
"text": "\n\nclass Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n A = sorted(nums)\n\n l, r = 1, len(nums)\n def can(mid):\n for i in range(mid):\n if A[i] >= A[len(nums) - mid + i]: return False\n return True\n\n\n while l <= r:\n mid = (l + r) // 2\n if can(mid):\n l = mid + 1\n else:\n r = mid - 1\n return r\n\n"
13839+
},
13840+
{
13841+
"language": "py",
13842+
"text": "\nclass Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n nums.sort()\n i = 0\n for x in nums:\n if x > nums[i]:\n i += 1\n return i\n\n"
13843+
}
13844+
]
13845+
},
13846+
"find-score-of-an-array-after-marking-all-elements":{
13847+
"id": "2593",
13848+
"name": "find-score-of-an-array-after-marking-all-elements",
13849+
"pre": [
13850+
{
13851+
"text": "哈希表",
13852+
"link": null,
13853+
"color": "gold"
13854+
}
13855+
],
13856+
"keyPoints": [
13857+
{
13858+
"text": "哈希表记录每个元素的访问状态",
13859+
"link": null,
13860+
"color": "blue"
13861+
}
13862+
],
13863+
"companies": [],
13864+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2593.find-score-of-an-array-after-marking-all-elements.md",
13865+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2593.find-score-of-an-array-after-marking-all-elements.md",
13866+
"code": [
13867+
{
13868+
"language": "py",
13869+
"text": "\n\nclass Solution:\n def findScore(self, nums: List[int]) -> int:\n ans = 0\n vis = [False] * (len(nums) + 2) # 保证下标不越界\n for i, x in sorted(enumerate(nums, 1), key=lambda p: p[1]):\n if not vis[i]:\n vis[i - 1] = True\n vis[i + 1] = True # 标记相邻的两个元素\n ans += x\n return ans\n\n"
13870+
}
13871+
]
13872+
},
1372913873
"selling-pieces-of-wood":{
1373013874
"id": "5254",
1373113875
"name": "selling-pieces-of-wood",
@@ -14323,12 +14467,33 @@
1432314467
"md":{
1432414468
"id": "Number-of-Substrings-with-Single-Character-Difference",
1432514469
"name": "md",
14326-
"pre": [],
14327-
"keyPoints": [],
14470+
"pre": [
14471+
{
14472+
"text": "动态规划",
14473+
"link": null,
14474+
"color": "red"
14475+
}
14476+
],
14477+
"keyPoints": [
14478+
{
14479+
"text": "建立前后缀dp数组,将问题转化为前后缀的笛卡尔积",
14480+
"link": null,
14481+
"color": "blue"
14482+
}
14483+
],
1432814484
"companies": [],
1432914485
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/Number-of-Substrings-with-Single-Character-Difference.md",
1433014486
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/Number-of-Substrings-with-Single-Character-Difference.md",
14331-
"code": []
14487+
"code": [
14488+
{
14489+
"language": "py",
14490+
"text": "\nclass Solution:\n def solve(self, s, t):\n ans = 0\n for i in range(len(s)):\n for j in range(len(t)):\n mismatches = 0\n for k in range(min(len(s) - i, len(t) - j)):\n mismatches += s[i + k] != t[j + k]\n if mismatches == 1:\n ans += 1\n elif mismatches > 1:\n break\n return ans\n\n"
14491+
},
14492+
{
14493+
"language": "py",
14494+
"text": "\n\nclass Solution:\n def solve(self, s, t):\n m, n = len(s), len(t)\n prefix = [[0] * (n + 1) for _ in range(m + 1)]\n suffix = [[0] * (n + 1) for _ in range(m + 1)]\n\n for i in range(1, m + 1):\n for j in range(1, n + 1):\n if s[i - 1] == t[j - 1]:\n prefix[i][j] = prefix[i - 1][j - 1] + 1\n\n for i in range(m - 1, -1, -1):\n for j in range(n - 1, -1, -1):\n if s[i] == t[j]:\n suffix[i][j] = suffix[i + 1][j + 1] + 1\n\n ans = 0\n for i in range(1, m + 1):\n for j in range(1, n + 1):\n if s[i - 1] != t[j - 1]:\n ans += (prefix[i - 1][j - 1] + 1) * (suffix[i][j] + 1)\n return ans\n\n"
14495+
}
14496+
]
1433214497
},
1433314498
"md":{
1433414499
"id": "Sort-String-by-Flipping",

0 commit comments

Comments
 (0)