Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value can you put into the backpack?

Note

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.
 
Solution:
 public class Solution {
/**
* @param m: An integer m denotes the size of a backpack
* @param A & V: Given n items with size A[i] and value V[i]
* @return: The maximum value
*/
public int backPackII(int m, int[] A, int V[]) {
int len = A.length;
if (len==0) return -1; int[][] maxVal = new int[len+1][m+1];
for (int i=0;i<=m;i++)
maxVal[0][i]=0; for (int i = 1; i<=len;i++)
for (int s=0; s<=m; s++){
maxVal[i][s] = maxVal[i-1][s];
if (s>=A[i-1] && maxVal[i][s]<maxVal[i-1][s-A[i-1]]+V[i-1])
maxVal[i][s] = maxVal[i-1][s-A[i-1]]+V[i-1];
} int max = 0;
for (int i=0;i<=m;i++)
if (maxVal[len][i]>max) max = maxVal[len][i]; return max; }
}

LintCode-BackPack II的更多相关文章

  1. [LintCode] Backpack VI 背包之六

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  2. LintCode "Backpack"

    A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...

  3. Backpack II

    Description There are n items and a backpack with size m. Given array A representing the size of eac ...

  4. [LintCode] Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. [LintCode]——目录

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

  6. Backpack | & ||

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

  7. leetcode Ch2-Dynamic Programming II

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

  8. [算法专题] 深度优先搜索&回溯剪枝

    1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...

  9. Java Algorithm Problems

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

  10. lintcode 最长上升连续子序列 II(二维最长上升连续序列)

    题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...

随机推荐

  1. poj3020

    define     n    the number of  ' * ' define     d    the number of couple of two points define     s ...

  2. oracle split

    select * from table(fun_strsplit('1,2,3,4,5')); 1.创建一个类型 ) 2.创建函数 CREATE OR REPLACE FUNCTION Fun_Str ...

  3. MFC创建对话框组件对应变量并进行设置值(VS2010)

    m_path = strFolderPath; UpdateData(FALSE);

  4. 搭建高性能计算环境(九)、应用软件的安装之gaussian 09

    高斯软件一般使用的都是编译好的二进制版,所以解压缩后设置一下环境变量就可以用了. cd /opt tar xvf g09.tar.gz 设置环境变量,添加到/etc/profile文件中,重新登录后生 ...

  5. Cookie禁用了,Session还能用吗?

    Cookie与Session,一般认为是两个独立的东西,Session采用的是在服务器端保持状态的方案,而Cookie采用的是在客户端保持状态的方案.Cookie分为两种,一种可以叫做session ...

  6. POJ C++程序设计 编程题#2 编程作业—文件操作与模板

    编程题#2: 实数的输出格式 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 ...

  7. Elipse安装Spring Tool Suite

    STS实际上是对Eclipse的Spring包装,下载STS IDE可以直接开发spring web. 但大多数人还是喜欢使用eclipse.下面就eclipse安装sts插件做个介绍. 1.首先到s ...

  8. NetCat使用手册

    简介:   在网络工具中有“瑞士军刀”美誉的NetCat(以下简称nc),在我们用了N年了至今仍是爱不释手.因为它短小精悍(这个用在它身上很适合,现在有人已经将其修改成大约10K左右,而且功能不减少) ...

  9. Laravel 5 基础(八)- 模型、控制器、视图基础流程

    添加路由 Route::get('artiles', 'ArticlesController@index'); 创建控制器 php artisan make:controller ArticlesCo ...

  10. Jquery在项目中的总结

    1.构造对象 var _getSearchArg = function () { var argModel = {}; argModel.Txt = value; argModel.Code = va ...