day44 动态规划part6 代码随想录算法训练营 卡尔网52题

题目:52. 携带研究材料

我的感悟:

  • 背模板,记下来,就好了。

理解难点:

听课笔记:

代码示例:

n, v = map(int, input().split())

weight_list = []
value_list = []
for _ in range(n):
    weight, value = map(int, input().split())
    weight_list.append(weight)
    value_list.append(value)
# print(n,v)
# print(weight_list, value_list)

def func(weight_list, value_list, bag_size):
    dp = [0] * (bag_size + 1)
    for i in range(len(weight_list)):   # 遍历物品
        for j in range(weight_list[i],bag_size + 1):    # 正序遍历背包
            dp[j] = max(dp[j], dp[j-weight_list[i]]+value_list[i])
    # print(dp)
    return dp[bag_size]

print(func(weight_list,value_list,v))

通过截图:

扩展写法:

资料:

完全背包 

视频讲解:https://www.bilibili.com/video/BV1uK411o7c9

https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85.html

posted @ 2024-02-28 12:25  o蹲蹲o  阅读(3)  评论(0编辑  收藏  举报