增加 背包理论基础01背包-2.md Python3 实现代码

This commit is contained in:
Kelvin
2021-06-15 21:06:42 -04:00
parent 0bafb2d775
commit ecd98c9a79

View File

@ -242,7 +242,24 @@ Java
Python Python
```python
def test_1_wei_bag_problem():
weight = [1, 3, 4]
value = [15, 20, 30]
bag_weight = 4
# 初始化: 全为0
dp = [0] * (bag_weight + 1)
# 先遍历物品, 再遍历背包容量
for i in range(len(weight)):
for j in range(bag_weight, weight[i] - 1, -1):
# 递归公式
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
print(dp)
test_1_wei_bag_problem()
```
Go Go
```go ```go