From f5b8f8faeb8aa5965e534ba41ae8a2ea378d20b2 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 13:57:42 -0400 Subject: [PATCH 1/5] =?UTF-8?q?=E6=9B=B4=E6=96=B00343.=E6=95=B4=E6=95=B0?= =?UTF-8?q?=E6=8B=86=E5=88=86.md=20python3=E4=BB=A3=E7=A0=81=20-=20?= =?UTF-8?q?=E5=B0=8F=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据讲解, dp[0], dp[1]不应该初始化. 如果 是for j in range(1, i): 递归方程就会需要dp[1]. 虽然也可以AC, 但是不合逻辑. --- problems/0343.整数拆分.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index fefaa293..cf60575f 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -218,7 +218,7 @@ class Solution: # 假设对正整数 i 拆分出的第一个正整数是 j(1 <= j < i),则有以下两种方案: # 1) 将 i 拆分成 j 和 i−j 的和,且 i−j 不再拆分成多个正整数,此时的乘积是 j * (i-j) # 2) 将 i 拆分成 j 和 i−j 的和,且 i−j 继续拆分成多个正整数,此时的乘积是 j * dp[i-j] - for j in range(1, i): + for j in range(1, i - 1): dp[i] = max(dp[i], max(j * (i - j), j * dp[i - j])) return dp[n] ``` From 6673c56916ac17ed48b7a30bffe3d8912773a0b1 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:12:08 -0400 Subject: [PATCH 2/5] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改indention. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 852c489b..36544256 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4; From 6a42937927f1e9c9c4925fa53332ddf4fe275d24 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:29:59 -0400 Subject: [PATCH 3/5] =?UTF-8?q?Update=20=E8=83=8C=E5=8C=85=E7=90=86?= =?UTF-8?q?=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md=20Python?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加python部分代码. 测试结果: ``` [[0, 15, 15, 15, 15], [0, 15, 15, 20, 35], [0, 15, 15, 20, 35]] ``` 与题解一致. --- problems/背包理论基础01背包-1.md | 35 ++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 36544256..1269d9c1 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -307,6 +307,41 @@ Java: Python: +```python +def test_2_wei_bag_problem1(bag_size, weight, value) -> int: + rows, cols = len(weight), bag_size + 1 + dp = [[0 for _ in range(cols)] for _ in range(rows)] + res = 0 + + # 初始化dp数组. + for i in range(rows): + dp[i][0] = 0 + first_item_weight, first_item_value = weight[0], value[0] + for j in range(1, cols): + if first_item_weight <= j: + dp[0][j] = first_item_value + + # 更新dp数组: 先遍历物品, 再遍历背包. + for i in range(1, len(weight)): + cur_weight, cur_val = weight[i], value[i] + for j in range(1, cols): + if cur_weight > j: # 说明背包装不下当前物品. + dp[i][j] = dp[i - 1][j] # 所以不装当前物品. + else: + # 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。 + dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val) + if dp[i][j] > res: + res = dp[i][j] + + print(dp) + + +if __name__ == "__main__": + bag_size = 4 + weight = [1, 3, 4] + value = [15, 20, 30] + test_2_wei_bag_problem1(bag_size, weight, value) +``` Go: From be2804f8740e5027354265d722d77e9e89e7fd0d Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:30:30 -0400 Subject: [PATCH 4/5] =?UTF-8?q?Revert=20"Update=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-1.md"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 6673c56916ac17ed48b7a30bffe3d8912773a0b1. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index 1269d9c1..f6646202 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4; From 737b7c38e86a1365b99f4bcc4ef354c04dd44955 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Sat, 12 Jun 2021 14:32:08 -0400 Subject: [PATCH 5/5] =?UTF-8?q?=E6=9B=B4=E6=94=B9=20=E8=83=8C=E5=8C=85?= =?UTF-8?q?=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=8001=E8=83=8C=E5=8C=85-2.md?= =?UTF-8?q?=20Java=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原版在github上显示不正确. --- problems/背包理论基础01背包-1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index f6646202..1269d9c1 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -268,7 +268,7 @@ int main() { Java: ```java - public static void main(String[] args) { + public static void main(String[] args) { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagSize = 4;