mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 背包理论基础01背包-2.md Python代码
增加python部分代码. 测试结果: ``` [[0, 15, 15, 15, 15], [0, 15, 15, 20, 35], [0, 15, 15, 20, 35]] ``` 与题解一致.
This commit is contained in:
@ -307,6 +307,41 @@ Java:
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
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:
|
Go:
|
||||||
|
Reference in New Issue
Block a user