We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9ce73dd commit ccf5054Copy full SHA for ccf5054
Solutions/0416. 分割等和子集.md
@@ -73,14 +73,17 @@ $dp[w] = \begin{cases} dp[w] & w < nums[i - 1] \cr max \lbrace dp[w], dp[w - num
73
74
```Python
75
class Solution:
76
- def zeroOnePackOptimization(self, weight: [int], value: [int], W: int):
+ def zeroOnePack(self, weight: [int], value: [int], W: int):
77
size = len(weight)
78
dp = [0 for _ in range(W + 1)]
79
80
+ # 枚举前 i 种物品
81
for i in range(1, size + 1):
82
+ # 逆序枚举背包装载重量(避免状态值错误)
83
for w in range(W, weight[i - 1] - 1, -1):
84
+ # dp[w] 取「前 i - 1 件物品装入载重为 w 的背包中的最大价值」与「前 i - 1 件物品装入载重为 w - weight[i - 1] 的背包中,再装入第 i - 1 物品所得的最大价值」两者中的最大值
85
dp[w] = max(dp[w], dp[w - weight[i - 1]] + value[i - 1])
-
86
+
87
return dp[W]
88
89
def canPartition(self, nums: List[int]) -> bool:
@@ -89,7 +92,7 @@ class Solution:
92
return False
90
93
91
94
target = sum_nums // 2
- return self.zeroOnePackOptimization(nums, nums, target) == target
95
+ return self.zeroOnePack(nums, nums, target) == target
96
```
97
98
### 思路 1:复杂度分析
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments