ToB企服应用市场:ToB评测及商务社交产业平台

标题: 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
  1. class Solution:
  2.     def change(self, amount: int, coins: List[int]) -> int:
  3.         m = len(coins)
  4.         dp = [0] * (amount + 1)
  5.         dp[0] = 1
  6.         for i in range(0, m):
  7.             for j in range(coins[i], amount + 1):
  8.                 dp[j] += dp[j-coins[i]]
  9.         
  10.         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.
  1. class Solution:
  2.     def combinationSum4(self, nums: List[int], target: int) -> int:
  3.         dp = [0] * (target + 1)
  4.         dp[0] = 1
  5.         # # combination
  6.         # for i in nums:
  7.         #     for j in range(i, target+1):
  8.         #         dp[j] += dp[j-i]
  9.         # permutation
  10.         for j in range(target+1):
  11.             for i in nums:
  12.                 if j >= i:
  13.                     dp[j] += dp[j-i]
  14.         
  15.         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.
  1. def climbing_stairs(n,m):
  2.     # here, m means the maximum stairs for each step
  3.     # n means how many stairs are there
  4.     # so every step, the stairs can be chosen from 1,2,3...,m
  5.     dp = [0]*(n+1) # 背包总容量
  6.     dp[0] = 1
  7.     # because this is permutation, so the outer loop should be capability
  8.     for j in range(1,n+1):
  9.         for i in range(1,m+1):
  10.             if j>=i:
  11.                 dp[j] += dp[j-i]
  12.     return dp[n]
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4