Description

There are n items and a backpack with size m. Given array A representing the size of each item and array V representing the value of each item.

What's the maximum value can you put into the backpack?

  1. A[i], V[i], n, m are all integers.
  2. You can not split an item.
  3. The sum size of the items you want to put into backpack can not exceed m.
  4. Each item can only be picked up once

Example

Example 1:

Input: m = 10, A = [2, 3, 5, 7], V = [1, 5, 2, 4]
Output: 9
Explanation: Put A[1] and A[3] into backpack, getting the maximum value V[1] + V[3] = 9

Example 2:

Input: m = 10, A = [2, 3, 8], V = [2, 5, 8]
Output: 10
Explanation: Put A[0] and A[2] into backpack, getting the maximum value V[0] + V[2] = 10

Challenge

O(nm) memory is acceptable, can you do it in O(m) memory?

思路:

经典的01背包问题, 资源分配型动态规划.

设定 f[i][j] 表示前 i 个物品装入大小为 j 的背包里, 可以获取的最大价值总和. 决策就是第i个物品装不装入背包, 所以状态转移方程就是 f[i][j] = max(f[i - 1][j], f[i - 1][j - A[i]] + V[i])

可以使用滚动数组优化空间至 O(m).

public class Solution {
/**
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @param V: Given n items with value V[i]
* @return: The maximum value
*/
public int backPackII(int m, int[] A, int[] V) {
int[][] dp = new int[A.length + 1][m + 1];
for (int i = 0; i <= A.length; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (A[i - 1] > j) {
dp[i][j] = dp[(i - 1)][j];
} else {
dp[i][j] = Math.max(dp[(i - 1)][j], dp[(i - 1)][j - A[i - 1]] + V[i - 1]);
}
}
}
return dp[A.length][m];
}
}

  

Backpack II的更多相关文章

  1. Backpack | & ||

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

  2. leetcode Ch2-Dynamic Programming II

    一. Longest Valid Parentheses 方法一.一维DP class Solution { public: int longestValidParentheses(string s) ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  5. 多重背包问题II

    多重背包问题II 总体积是m,每个小物品的体积是A[i] ,每个小物品的数量是B[i],每个小物品的价值是C[i] 求能够放入背包内的最大物品能够获得的最大价值 和上一个很类似 上一题体积就是价值,这 ...

  6. lintcode:背包问题II

    背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...

  7. lintcode-125-背包问题 II

    125-背包问题 II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分. ...

  8. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  9. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

随机推荐

  1. 20191213-RF中报告打不开提示Opening Robot Framework report failed

    配置好Jenkins的RF框架后跑了一次autotest发现哦豁report打不开,网上找了一堆方法都是只能临时解决,重启后又失效了.现在给出临时解决方案和永久解决方案 首先错误信息如下:  临时解决 ...

  2. 阅读笔记——《How a Facebook rejection pushed me to start and grow a profitable business in 12 months》

    阅读笔记——<How a Facebook rejection pushed me to start and grow a profitable business in 12 months> ...

  3. JVM运行时内存结构学习

    学习JVM运行模型比较重要,先看一幅图片: 运行时数据区(内存结构) :  1.方法区(Method Area)类的所有字段和方法字节码,以及一些特殊方法如构造函数,接口代码也在这里定义.简单来说,所 ...

  4. git 学习笔记 ---撤销修改

    自然,你是不会犯错的.不过现在是凌晨两点,你正在赶一份工作报告,你在readme.txt中添加了一行: $ cat readme.txt Git is a distributed version co ...

  5. Java线程synchronized(一)

    线程安全概念:当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. synchronized:可以在任意对象及方法上加锁,而加锁的这段代码 ...

  6. fastDFS的入门程序

    导入jar包 <dependency> <groupId>cn.bestwu</groupId> <artifactId>fastdfs-client- ...

  7. jmeter中assertion的使用

    用于检查测试中得到的响应数据等是否符合预期,用以保证性能测试过程中的数据交互与预期一致. 最新版本的3.0jmeter中有13种不同的断言: 1)BeanShell断言:针对sampler中的Bean ...

  8. java web编程 servlet读取配置文件参数

    新建一个servlet. 然后在web.xml文件里面自动帮助你创建好了<servlet-name><servlet-class><servlet-mapping> ...

  9. Js获取url问号(View_Detail?data='+data.zjb_ID+'&'+data.D_Name)传值

    Js逻辑 View_Detail?data='+data.zjb_ID+'&'+data.D_Name <script> $(function () { var url = dec ...

  10. Oracle 多表插入

    多表插入 作用:一条INSERT语句可以完成向多张表的插入任务(Multitable insert).有两种形式:insert all与insert first,准备测试环境:1.创建表T并初始化测试 ...