零钱兑换
零钱作为物品 ,零钱的面额作为物品的重量
def change(self, amount, coins):""":type amount: int:type coins: List[int]:rtype: int"""dp = [0]*(amount + 1)dp[0] = 1# 遍历物品for i in range(len(coins)):# 遍历背包for j in range(coins[i], amount + 1):dp[j] += dp[j - coins[i]]return dp[amount]
组合总和
求排列总和数(动态规划,外层遍历背包,内层遍历物品)
不用全部把排列情况全部列出来(回溯)
def combinationSum4(self, nums: List[int], target: int) -> int:dp = [0] * (target + 1)dp[0] = 1for i in range(1, target + 1): # 遍历背包for j in range(len(nums)): # 遍历物品if i - nums[j] >= 0:dp[i] += dp[i - nums[j]]return dp[target]