From 3193d16f53372a6bd9d294538a83ef3e5d92a87f Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 14:53:01 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=20Python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0279.完全平方数.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index f7c06dbd..1b29626f 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -271,7 +271,27 @@ class Solution: # 返回结果 return dp[n] +``` +```python +class Solution(object): + def numSquares(self, n): + # 先把可以选的数准备好,更好理解 + nums, num = [], 1 + while num ** 2 <= n: + nums.append(num ** 2) + num += 1 + # dp数组初始化 + dp = [float('inf')] * (n + 1) + dp[0] = 0 + # 遍历准备好的完全平方数 + for i in range(len(nums)): + # 遍历背包容量 + for j in range(nums[i], n+1): + dp[j] = min(dp[j], dp[j-nums[i]]+1) + # 返回结果 + return dp[-1] + ``` ### Go: From 6dd9d117ffc60cb6854f26814b05aa5be2033e84 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Tue, 2 Apr 2024 23:58:58 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00139.=E5=8D=95=E8=AF=8D?= =?UTF-8?q?=E6=8B=86=E5=88=86=20Python=20DP=E5=89=AA=E6=9E=9D=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index a3d59ec7..5ad0fcb0 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -394,7 +394,28 @@ class Solution: dp[j] = dp[j] or (dp[j - len(word)] and word == s[j - len(word):j]) return dp[len(s)] ``` +DP(剪枝) +```python +class Solution(object): + def wordBreak(self, s, wordDict): + + # 先对单词按长度排序 + wordDict.sort(key=lambda x: len(x)) + n = len(s) + dp = [False] * (n + 1) + dp[0] = True + # 遍历背包 + for i in range(1, n + 1): + # 遍历单词 + for word in wordDict: + # 简单的 “剪枝” + if len(word) > i: + break + dp[i] = dp[i] or (dp[i - len(word)] and s[i - len(word): i] == word) + return dp[-1] + +``` ### Go: From fda18c06b7221b1c7bddd6b4115de5549e4fd5a4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Wed, 3 Apr 2024 22:23:48 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=E5=A4=9A?= =?UTF-8?q?=E9=87=8D=E8=83=8C=E5=8C=85=20Python=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../背包问题理论基础多重背包.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index 5d6440e3..29f157fb 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -204,6 +204,29 @@ class multi_pack{ ``` ### Python: +```python + +C, N = input().split(" ") +C, N = int(C), int(N) + +# value数组需要判断一下非空不然过不了 +weights = [int(x) for x in input().split(" ")] +values = [int(x) for x in input().split(" ") if x] +nums = [int(x) for x in input().split(" ")] + +dp = [0] * (C + 1) +# 遍历背包容量 +for i in range(N): + for j in range(C, weights[i] - 1, -1): + for k in range(1, nums[i] + 1): + # 遍历 k,如果已经大于背包容量直接跳出循环 + if k * weights[i] > j: + break + dp[j] = max(dp[j], dp[j - weights[i] * k] + values[i] * k) +print(dp[-1]) + +``` + ### Go: From 1c4b9a252fd004a9ec3771b6dbfc707818e4f1d4 Mon Sep 17 00:00:00 2001 From: matthew <1123957599@qq.com> Date: Sat, 6 Apr 2024 10:38:12 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A00583=20=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=88=A0=E9=99=A4=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=20Python=E8=A7=A3=E6=B3=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0583.两个字符串的删除操作.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0583.两个字符串的删除操作.md b/problems/0583.两个字符串的删除操作.md index 7dbb8ef5..4816c37c 100644 --- a/problems/0583.两个字符串的删除操作.md +++ b/problems/0583.两个字符串的删除操作.md @@ -234,6 +234,25 @@ class Solution: return dp[-1][-1] ``` +> 版本 2 + +```python +class Solution(object): + def minDistance(self, word1, word2): + m, n = len(word1), len(word2) + + # dp 求解两字符串最长公共子序列 + dp = [[0] * (n+1) for _ in range(m+1)] + for i in range(1, m+1): + for j in range(1, n+1): + if word1[i-1] == word2[j-1]: + dp[i][j] = dp[i-1][j-1] + 1 + else: + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + + # 删去最长公共子序列以外元素 + return m + n - 2 * dp[-1][-1] +``` ### Go: ```go