mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -315,7 +315,6 @@ Python:
|
|||||||
def test_2_wei_bag_problem1(bag_size, weight, value) -> int:
|
def test_2_wei_bag_problem1(bag_size, weight, value) -> int:
|
||||||
rows, cols = len(weight), bag_size + 1
|
rows, cols = len(weight), bag_size + 1
|
||||||
dp = [[0 for _ in range(cols)] for _ in range(rows)]
|
dp = [[0 for _ in range(cols)] for _ in range(rows)]
|
||||||
res = 0
|
|
||||||
|
|
||||||
# 初始化dp数组.
|
# 初始化dp数组.
|
||||||
for i in range(rows):
|
for i in range(rows):
|
||||||
@ -334,8 +333,6 @@ def test_2_wei_bag_problem1(bag_size, weight, value) -> int:
|
|||||||
else:
|
else:
|
||||||
# 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。
|
# 定义dp数组: dp[i][j] 前i个物品里,放进容量为j的背包,价值总和最大是多少。
|
||||||
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - cur_weight]+ cur_val)
|
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)
|
print(dp)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user