标题: day37-dynamic programming-part05-8.8 [打印本页] 作者: 拉不拉稀肚拉稀 时间: 2024-8-9 02:43 标题: 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], complete ascending[weigh, 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]
2.the initialization of dp is all 0 except the dp[0] = 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.
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