day37-dynamic programming-part05-8.8

tasks for today:

1. 完全背包理论基础

2. 518.零钱兑换II

3. 377.组合总和IV

4. 爬楼梯

---------------------------------------------------------------------------

1. 完全背包理论基础

完全背包和01背包问题唯一不同的地方就是,每种物品有无限件

As for the complete backpack problem, the key different is the objects in the list is infinite.

As for the code, the key change is the sequence of the inner loop for traversing the backpack capability, which should be reverse against that in 01 backpack problem. This is mainly caused by the inifiniteness of the objects.

Besides, in complete backpack problem, for 1-dim dp the order for object-capability or capability-object is the same, while in 01 backpack, should be object-capability.

But for 2-dim dp, both 01 or complete can be object-capability or capability-object.

So to be organized, we decide always to adhere to object-capability, 1-dim, outer loop asending [0-m], inner loop: 01 descending[n, weight[i]], complete ascending[weigh[i], n];

but the above discussion is based on backpack problem, which is a combination problem, when it comes to permutation problem, which is essentially not a backpack problem, if you still wanna use the dp method, for 1-dim dp, the loop order should be capability-object

2. 518.零钱兑换II

When the return value is the number of combination, pay attention to following two issue:

1.the recursive equation is dp[j] += dp[j-coins[i]]

2.the initialization of dp is all 0 except the dp[0] = 1

class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        m = len(coins)
        dp = [0] * (amount + 1)
        dp[0] = 1
        for i in range(0, m):
            for j in range(coins[i], amount + 1):
                dp[j] += dp[j-coins[i]]
        
        return dp[-1]

3. 377.组合总和IV

The difference between this practice and the practice 518 is that, 518 is combination problem while 377 is the permutation problem, so the outter loop should be capability while the inner loop should be object.

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp = [0] * (target + 1)
        dp[0] = 1
        # # combination
        # for i in nums:
        #     for j in range(i, target+1):
        #         dp[j] += dp[j-i]

        # permutation
        for j in range(target+1):
            for i in nums:
                if j >= i:
                    dp[j] += dp[j-i]
        
        return dp[-1]

4. 爬楼梯

when we first meet this practice, it is a simple FB list problem, now it can be seen as a complete pack problem, but with permutation mode, instead of combiantion mode.

This practice would be similar to the practice 377.

def climbing_stairs(n,m):
    # here, m means the maximum stairs for each step
    # n means how many stairs are there
    # so every step, the stairs can be chosen from 1,2,3...,m
    dp = [0]*(n+1) # 背包总容量
    dp[0] = 1 
    # because this is permutation, so the outer loop should be capability
    for j in range(1,n+1):
        for i in range(1,m+1):
            if j>=i:
                dp[j] += dp[j-i]
    return dp[n]
  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值