第九章 动态规划part06
● 完全背包
● 518. 零钱兑换 II
● 377. 组合总和 Ⅳ
详细布置
力扣上没有纯粹的完全背包的题目,所以大家看本篇了解一下 完全背包的理论
后面的两道题目,都是完全背包的应用,做做感受一下
完全背包
视频讲解: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
518. 零钱兑换 II
视频讲解:https://www.bilibili.com/video/BV1KM411k75j
https://programmercarl.com/0518.%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2II.html
377. 组合总和 Ⅳ
视频讲解:https://www.bilibili.com/video/BV1V14y1n7B6
https://programmercarl.com/0377.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%E2%85%A3.html
往日任务
● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY
● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG
● day 3 任务以及具体安排:https://docs.qq.com/doc/DUGdqYWNYeGhlaVR6
● day 4 任务以及具体安排:https://docs.qq.com/doc/DUFNjYUxYRHRVWklp
● day 5 周日休息
● day 6 任务以及具体安排:https://docs.qq.com/doc/DUEtFSGdreWRuR2p4
● day 7 任务以及具体安排:https://docs.qq.com/doc/DUElCb1NyTVpXa0Jj
● day 8 任务以及具体安排:https://docs.qq.com/doc/DUGdsY2JFaFhDRVZH
● day 9 任务以及具体安排:https://docs.qq.com/doc/DUHVXSnZNaXpVUHN4
● day 10 任务以及具体安排:https://docs.qq.com/doc/DUElqeHh3cndDbW1Q
●day 11 任务以及具体安排:https://docs.qq.com/doc/DUHh6UE5hUUZOZUd0
●day 12 周日休息
●day 13 任务以及具体安排:https://docs.qq.com/doc/DUHNpa3F4b2dMUWJ3
●day 14 任务以及具体安排:https://docs.qq.com/doc/DUHRtdXZZSWFkeGdE
●day 15 任务以及具体安排:https://docs.qq.com/doc/DUHN0ZVJuRmVYeWNv
●day 16 任务以及具体安排:https://docs.qq.com/doc/DUHBQRm1aSWR4T2NK
●day 17 任务以及具体安排:https://docs.qq.com/doc/DUFpXY3hBZkpabWFY
●day 18 任务以及具体安排:https://docs.qq.com/doc/DUFFiVHl3YVlReVlr
●day 19 周日休息
●day 20 任务以及具体安排:https://docs.qq.com/doc/DUGFRU2V6Z1F4alBH
●day 21 任务以及具体安排:https://docs.qq.com/doc/DUHl2SGNvZmxqZm1X
●day 22 任务以及具体安排:https://docs.qq.com/doc/DUHplVUp5YnN1bnBL
●day 23 任务以及具体安排:https://docs.qq.com/doc/DUFBUQmxpQU1pa29C
●day 24 任务以及具体安排:https://docs.qq.com/doc/DUEhsb0pUUm1WT2NP
●day 25 任务以及具体安排:https://docs.qq.com/doc/DUExTYXVzU1BiU2Zl
●day 26 休息
●day 27 任务以及具体安排:https://docs.qq.com/doc/DUElpbnNUR3hIbXlY
●day 28 任务以及具体安排:https://docs.qq.com/doc/DUG1yVHdlWEdNYlhZ
●day 29 任务以及具体安排:https://docs.qq.com/doc/DUHZYbWhwSHRCRmp3
●day 30 任务以及具体安排:https://docs.qq.com/doc/DUEdTVVhxbnJiY3BR
●day 31 任务以及具体安排:https://docs.qq.com/doc/DUG1PQ1ZZY2xXY1ly
●day 32 任务以及具体安排:https://docs.qq.com/doc/DUGFEdGFWeVhleFF1
●day 33 周日休息
●day 34 任务以及具体安排:https://docs.qq.com/doc/DUEh5WFVlQkp1U0p4
●day 35 任务以及具体安排:https://docs.qq.com/doc/DUFRWc3BGRHFXZ1pO
●day 36 任务以及具体安排:https://docs.qq.com/doc/DUERGbnhhRkFRVENZ
●day 37 任务以及具体安排:https://docs.qq.com/doc/DUFVRd3p5SHFMSExQ
●day 38 任务以及具体安排:https://docs.qq.com/doc/DUGNUdVpoT0VJR01l
●day 39 任务以及具体安排:https://docs.qq.com/doc/DUE55cVJ5WkNoREhS
●day 40 周日休息
●day 41 任务以及具体安排:https://docs.qq.com/doc/DUFhIUXRFYnVGUkFp
●day 42 任务以及具体安排:42 第八章 动态规划
●day 43 任务以及具体安排:43第八章 动态规划
day43
完全背包
//从前往后遍历,一个物品i会被多次判断是否可以j-weight[i] 再继续添加,所以可以添加多次 //两种遍历顺序的差异只是在于如何更新背包容量对应的最大价值,但最终的最大值是不变的,都是比较dp[j - weight[i] + value[i]] //遍历顺序没太搞明白,在下一题中,先容量再物品得到的是排列数,计算更多 private static void testCompletePack() { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagWeight = 4; int[] dp = new int[bagWeight + 1]; for (int i = 0; i < weight.length; i++) {//遍历物品 for (int j = weight[i]; j <= bagWeight; j++) {//遍历背包容量 dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i]); } } for (int maxValue : dp) { System.out.println(maxValue + " "); } } //先遍历背包,再遍历物品 private static void testCompletePackAnotherWay() { int[] weight = {1, 3, 4}; int[] value = {15, 20, 30}; int bagWeight = 4; int[] dp = new int[bagWeight + 1]; for (int i = 1; i <= bagWeight; i++) {//遍历背包容量 for (int j = 0; j < weight.length; j++) {//遍历物品 if (i - weight[j] >= 0) { dp[i] = Math.max(dp[i], dp[i - weight[j]] + value[j]); } } } for (int maxValue : dp) { System.out.println(maxValue + " "); } }
零钱兑换2
class Solution { public int change(int amount, int[] coins) { //递推表达式 int[] dp = new int[amount + 1]; //初始化dp数组,表示金额为0时只有一种情况,也就是什么都不装 dp[0] = 1; for (int i = 0; i < coins.length; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } }
组合总和4
class Solution { public int combinationSum4(int[] nums, int target) { int[] dp = new int[target + 1]; dp[0] = 1; //求排列数 for(int i = 0; i <= target ; i++){//先遍历背包,再遍历数字 for(int j = 0; j < nums.length; j++){ if(i>=nums[j]){ dp[i] += dp[i-nums[j]]; } } } return dp[target]; } }
感谢大佬分享: