标记:

动态规划

问题描述:

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

Example

If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select [2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

解题方法:

快要死了,自己想了将近3个小时,居然卡在了如何保持物品放入的唯一性上了,中间用了标记位,哈希集的方法来解决都宣告失败。这一道题还是还是典型的动态规划的背包问题。今天已经可以很自然的划分出子问题来了,并且可以想到利用矩阵的数据结构来存储子状态,并且利用子状态的解来进一步解决问题。但是关于子问题之间的关系和递推方程的领悟还需要进一步的练习,毕竟动态规划是最难的,不是可能轻易领悟的。再一次向九章算法的答案致敬(真是太牛逼了!如果不看这个答案想死了我也想不出这样的解法来。)

好的言归正传,下面是解题思路:

1.划分子问题:

这一题的主问题是什么?非常典型的背包问题的变种。一列物品放入背包之中怎么可以使放入物品达到最大重量,这一问题与先前解决的问题最大的不同在于所有放入的物品是唯一的,就是只能放一次。当然子问题划分与之前并没有什么很大的变化,还是从物品个数和最大重量的两个向量上来划分。列出物品数量*最大重量的矩阵dp[0....i][0....m],期中dp[i][j]所代表的意义在于在最大重量为j时,同时又i种物品可以选,是否存在恰好的有效解,即正好可以有几样物品的重量之和等于当前所给的最大重量。所以dp这一矩阵的数据类型设置为boolean。

2.初始状态定义:

对于所有情况开始都是未知,所以dp整个矩阵可以定义为false,即开始都不存在恰好的解决方案

dp[0][0] = true 作为一个恒成立的情况,即0个物品0个重量时是存在的。

3.子问题之间的关系(递推方程):

这一部分也是所有动态规划最难,最精妙的地方,一个人的逻辑如何,算法能力如何,基本都体现在这一步。

如果在重量j上存在j大于当前物品集合A[i]的重量且在前一物品集合中j-A[i]的位置上也存在恰好解:

例如:[3,4,8,5]四个物品最大的重量时10

0.在0个物品中,重量为0时存在恰好解

1.在只有3的一个物品的情况下重量为0,3时存在恰好解。

2.在存在3,4两个物品的情况下重量为3,4,7时存在恰好解

3.在存在3,4,8三个物品时重量为3,4,7时存在恰好解

4.在存在3,4,8,5三个物品重量为3,4,7,9时存在恰好解

在2中判断重量为7时是否存在恰好解会先判断1中7-4=3的位置是否存在恰好解,若存在此处恰好加上物品4等于7.

若不满足要求则与前一物品情况相同

4.终止状态:

当遍历完全部情况以后,再对存在所有物品的情况中,在不同重量上进行遍历,返回存在恰好解的最大重量,则为题目的解。

5.参考代码:

 public int backPack(int m, int[] A) {
// write your code here boolean[][] dp = new boolean[A.length+1][m+1]; for(int i = 0; i<=A.length; i++){
for(int j = 0; j<=m; j++){
dp[i][j] = false;
}
}
dp[0][0] = true; for(int i = 0; i<A.length; i++){
for(int j=0; j<=m; j++){
dp[i+1][j] = dp[i][j];
if(j>=A[i]&&dp[i][j-A[i]]){
dp[i+1][j] = true;
}
}
} for(int i=m; i>=0; i--){
if(dp[A.length][i]){
return i;
}
}
return 0;
}

LintCode刷题笔记-- BackpackII的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- Distinct Subsequences

    标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...

  8. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  9. LintCode刷题笔记-- BackpackIII

    标签:动态规划 问题描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximu ...

随机推荐

  1. fastjson对Date类型的格式化

    @JSONField(format="yyyy-MM-dd HH:mm:ss.SSS") private Date sendMqDate; //MQ发送时间

  2. 牛客NOIP暑期七天营-提高组6

    目录 A-积木大赛 题目描述 link 题解 代码 B-破碎的序列 题目描述 link 题解 C-分班问题 题目描述 link 题解 比赛链接 官方题解 A-积木大赛 题目描述 link 题解 标签: ...

  3. Lucene 评分机制二 Payload

    这里使用的Lucene4.7.0和Lucene3.X稍有不同 有下面三段内容,我想对船一系列的搜索进行加分 bike car jeep truck bus boat train car ship bo ...

  4. (转)AngularJS中使用的表单验证

    原文  http://www.cnblogs.com/woshinidezhu/p/Form-validation-with-AngularJS.html 客户端表单验证是AngularJS里面最酷的 ...

  5. Weekly Challenges - Week 11

    一拖再拖,忍无可忍,自己懒的没救了. 一周前就结束的比赛,到现在才想起来补. 最后一题貌似又遇到了splay...还是不会!!!shit 题目链接 A题--快速幂 #include <cmath ...

  6. light oj 1079 01背包

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...

  7. 加载框(loading)

    一般在用户提交数据或者新加载页面时,请求服务器的过程,页面没有响应,但是用户并不知道,此时在发生什么.这时,就需要loading框给用户提示,增加用户体验. 1.引入loading.css. html ...

  8. Leetcode429.N-ary Tree Level Order TraversalN叉树的层序遍历

    给定一个 N 叉树,返回其节点值的层序遍历. (即从左到右,逐层遍历). 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6] ] 说明: 树的深度不会超过 100 ...

  9. 跟我一起做一个vue的小项目(九)

    接下来我们进行的就是城市列表页面数据额动态渲染. 也是在mock数据,进行动态渲染 //city.json { "ret": true, "data":{ &q ...

  10. linux中tab键不能补全,却能切换窗口

    linux中所有程序-设置-窗口管理器-键盘-切换同一应用程序的窗口-清除