LintCode刷题笔记-- BackpackII
标记:
动态规划
问题描述:
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的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- Distinct Subsequences
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackIII
标签:动态规划 问题描述: Given n items with size Ai and value Vi, and a backpack with size m. What's the maximu ...
随机推荐
- 力扣算法题—146LRU缓存机制
[题目] 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果密钥 (k ...
- WhaleCTF之隐写-Find
WhaleCTF之隐写-Find 前往题目 图片保存到本地,用Stegsolve打开图片 找到二维码 用微信或qq扫描,得到flag~
- 《DSP using MATLAB》Problem 8.17
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- IDEA中log4j 无法输出到本地,properties配置无效问题。
log4j添加以后无法输出日志信息,经检查(按以下顺序): 1.jar包导入正常 2.log4j.properties配置文件正常 3.logger.info可以输出,但是properties文件无效 ...
- python web 分页组件
闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...
- 在mac下怎么配置web环境(php)
1, 安装PHP+apach+mysql(xampp) 2, 在目录下建一个新文件夹 : 我是在Users/个人目录/workspace 3, 打开/Applications/XAMPP/xamp ...
- Create STKNetDiskC Instance Error
关于Create STKNetDiskC Instance Error错误的解决方法 这个错误可能出现在: AM8 附件直接存网盘的时候 报错 解决方法步骤如下: 在出现该错误的机器上,先将AM及 m ...
- Ionic 包名修改 步骤
1.config.xml => <widget id=...... 2.plugin 中 android.json 里面package 3.platforms\android 里面 and ...
- 不同目录cookie共享的问题解决 cookie不同页面访问不到的问题
一般设置cookie的方法是setcookie(key, value, expire),参数分别的意思是建.值.过期时间,这里是大众的默认设置方法,但是忽略了一个问题,setcookie还有path与 ...
- 深入浅出 Java Concurrency (5): 原子操作 part 4[转]
在JDK 5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁(后面的章节还会谈到锁). 锁机制存在以下问题: (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度 ...