Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccf5054

Browse files
committed
Update 0416. 分割等和子集.md
1 parent 9ce73dd commit ccf5054

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

‎Solutions/0416. 分割等和子集.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,17 @@ $dp[w] = \begin{cases} dp[w] & w < nums[i - 1] \cr max \lbrace dp[w], dp[w - num
7373

7474
```Python
7575
class Solution:
76-
def zeroOnePackOptimization(self, weight: [int], value: [int], W: int):
76+
def zeroOnePack(self, weight: [int], value: [int], W: int):
7777
size = len(weight)
7878
dp = [0 for _ in range(W + 1)]
7979

80+
# 枚举前 i 种物品
8081
for i in range(1, size + 1):
82+
# 逆序枚举背包装载重量(避免状态值错误)
8183
for w in range(W, weight[i - 1] - 1, -1):
84+
# dp[w] 取「前 i - 1 件物品装入载重为 w 的背包中的最大价值」与「前 i - 1 件物品装入载重为 w - weight[i - 1] 的背包中,再装入第 i - 1 物品所得的最大价值」两者中的最大值
8285
dp[w] = max(dp[w], dp[w - weight[i - 1]] + value[i - 1])
83-
86+
8487
return dp[W]
8588

8689
def canPartition(self, nums: List[int]) -> bool:
@@ -89,7 +92,7 @@ class Solution:
8992
return False
9093

9194
target = sum_nums // 2
92-
return self.zeroOnePackOptimization(nums, nums, target) == target
95+
return self.zeroOnePack(nums, nums, target) == target
9396
```
9497

9598
### 思路 1:复杂度分析

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /