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. Kafka-Docker:使用Docker运行Apache Kafka的步骤

    1.目标 在这个Kafka教程中,我们将学习Kafka-Docker的概念.此外,我们将在Kafka中看到Docker的卸载过程.这包括使用Docker 运行Apache Kafka的所有步骤  .除 ...

  2. Python各个岗位的开发流程

     根据张大美女提供资料微修改,在这谢谢张大美女! 1.python软件开发工程师 1.1 项目启动会 说明项目目标.阶段划分.组织结构.管理流程等关键事项. 1.2 需求调研 由用户提出,描述产品的功 ...

  3. Python简介及开发环境搭建

    Python简介 Python是一门动态解释性的强类型定义的计算机程序设计语言,是一种完全面向对象的语言,由荷兰人"龟叔"-Guido van Rossum于1989年开发,于19 ...

  4. css之多行居中

    需求: 单行到多行文字居中. <div> <p>应该为数组中的每个子代分配一个唯一的键.表格dataSource和中的值columns应遵循此规则.默认情况下</p> ...

  5. redis连接相关命令

    命令名称:echo 语法:echo message 功能: 打印一个特定的信息message,测试时使用. 返回值: message自身 命令名称:ping 语法:ping 功能: 使用客户端向red ...

  6. Maven过滤属性文件,替换属性值

    pom.xml 1.resources: resources中是定义哪些目录下的文件会被配置文件中定义的变量替换,一般我们会把项目的配置文件放在src/main/resources下,像db,bean ...

  7. 【阿里云开发】- 安装MySQL数据库

    我用的机器配置是 阿里云轻量服务器,系统:CentOS7.3,内存:2G,系统盘40G,1核. 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQ ...

  8. ES6- - Map与Set

    Map和Set 是 ES6 中新增的一种数据结构.Map为类似于Object的键值对结构,Set为成员唯一的类数组结构.以Map为例介绍两种数据结构的遍历方法.for...of var map = n ...

  9. html,css,js(包含简单的 ES6语法) 实现 简单的音乐盒

    知识要点 videoObject.load(): 加载某个视频(音频)文件,即重新播放 videoObject.play(): 播放视频(音频)文件 videoObject.remove(): 停止播 ...

  10. SQL SERVER-日期按时区转换

    SELECT SWITCHOFFSET('2019-07-19 08:35:06.637','+08:00')