LintCode-BackPack II
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?
You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.
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的更多相关文章
- [LintCode] Backpack VI 背包之六
Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...
- LintCode "Backpack"
A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...
- Backpack II
Description There are n items and a backpack with size m. Given array A representing the size of eac ...
- [LintCode] Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- Backpack | & ||
Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...
- leetcode Ch2-Dynamic Programming II
一. Longest Valid Parentheses 方法一.一维DP class Solution { public: int longestValidParentheses(string s) ...
- [算法专题] 深度优先搜索&回溯剪枝
1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
随机推荐
- SQL 2008配置管理工具服务显示 远程过程调用失败0x800706be
摘自: http://www.cnblogs.com/cool-fire/archive/2012/09/15/2686131.html 基本上我的解决方案也是根据该文提示 操作的. 因为 我后来 装 ...
- ON DUPLICATE KEY UPDATE用法
INSERT INTO `books ` (`name`,`count`,`num`) VALUES ('windows','1','2'),('','linux','1','3') ON DUPLI ...
- dig out secrets beneath AirSig
My sister installed AirSig last week. She is so exciting about this new techknology and she won't st ...
- 二十二、OGNL的一些其他操作
二十二.OGNL的一些其他操作 投影 ?判断满足条件 动作类代码: ^ $ public class Demo2Action extends ActionSupport { public ...
- 从手机获取图片让WebView支持本地上传图片
一,从本地获取相册中的图片,并获取图片的URI 从本地选择图片上传到服务器时,首先要打开本地图片或文件管理器选择要上传的文件,代码如下 Intent intent =newIntent(Intent. ...
- OpenStack实战(一)
OpenStack作为当前发展势头迅猛的云计算开源项目,去年进行了一些了解,现在有空回来进行一些补充记录,当时实战的版本是那会最新版本,当然现在已经更新了好几版了,不过还是那句话“这些丝毫不影响,了解 ...
- centos6.7下网络设置
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static" # ...
- tp框架集成支付宝,中转页变成gbk编码
tp框架中集成支付宝的功能,将支付宝的demo例子存在到下图位置\Extend\Vendor\Alipay 生成支付订单 /** * 支付订单 */ public function pay() { h ...
- Laravel 5 基础(十二)- 认证
Laravel 出厂已经带有了用户认证系统,我们来看一下 routes.php,如果删除了,添加上: Route::controllers([ 'auth' => 'Auth\AuthContr ...
- 【Delphi】注册快捷键
ShortCutToText , TextToShortCut 需 uses Menus; type TForm1 = class(TForm) HotKey1: THotKey; Button1: ...