|
3444 | 3444 | "language": "js",
|
3445 | 3445 | "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"
|
3446 | 3446 | },
|
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 |
| - }, |
3451 | 3447 | {
|
3452 | 3448 | "language": "cpp",
|
3453 | 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"
|
|
12801 | 12797 | }
|
12802 | 12798 | ]
|
12803 | 12799 | },
|
| 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 | +}, |
12804 | 12844 | "create-sorted-array-through-instructions":{
|
12805 | 12845 | "id": "1649",
|
12806 | 12846 | "name": "create-sorted-array-through-instructions",
|
@@ -13726,6 +13766,110 @@
|
13726 | 13766 | }
|
13727 | 13767 | ]
|
13728 | 13768 | },
|
| 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 | +}, |
13729 | 13873 | "selling-pieces-of-wood":{
|
13730 | 13874 | "id": "5254",
|
13731 | 13875 | "name": "selling-pieces-of-wood",
|
@@ -14323,12 +14467,33 @@
|
14323 | 14467 | "md":{
|
14324 | 14468 | "id": "Number-of-Substrings-with-Single-Character-Difference",
|
14325 | 14469 | "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 | + ], |
14328 | 14484 | "companies": [],
|
14329 | 14485 | "giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/Number-of-Substrings-with-Single-Character-Difference.md",
|
14330 | 14486 | "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 | + ] |
14332 | 14497 | },
|
14333 | 14498 | "md":{
|
14334 | 14499 | "id": "Sort-String-by-Flipping",
|
|
0 commit comments