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 ...
随机推荐
- console.log在线调试
前端开发人员工作有时候会用到console.log,PC端直接能打开开发者工具.但是移动端就不太方便了,为此提供一种简单的方法,只需2步: 1.打开http://jsconsole.com/ 输入: ...
- java值得注意的几个问题
1.一个源文件中只能有一个类是public的,其他的都是默认权限的: 2.一个类只能作为public或者默认权限(就是没有修饰符的意思): 3.源文件的public类的名字必须要跟文件名保持一致,否则 ...
- Windowsphone本地应用信息与市场信息的获取
本地信息都存放在 WMAppManifest 里面,获取就不用说了...知道位置 就知道怎么获取了.. 主要是讲那个 市场上面的详情怎么获取,就是API调用显示在这个页面里面的详情: public v ...
- 显示和隐藏Mac隐藏文件的终端命令
打开终端,输入以下命令: 显示mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏mac隐藏文件的命令 ...
- ISBN和标准编码关系以及概念
<中国标准书号>(China standard Book Number)是1986年由国家标准局批准颁发的一项国家标准(GB5795-86).该标准是在采用国际标准LSO2108——国际标 ...
- div+css+jQuery图片横向滚动代码(带左右点击按钮)
首先感谢Blue老师的javascript教程,给了我很多的启发,这是我在看完10 - 定时器的使用 - 2这节视频后,自己试着用jQuery重新改写了一下代码,感觉至少比百度搜出来的那一坨靠谱多了, ...
- luigi学习1
一.luigi介绍 luigi是基于python语言的,可帮助建立复杂流式批处理任务管理系统.这些批处理作业典型的有hadoop job,数据库数据的导入与导出,或者是机器学习算法等等. luigi的 ...
- mac里边配置android开发环境,intellij开发工具:
1 在android的官网下载 android sdk的mac版 http://developer.android.com/sdk/index.html 选择mac的版本 下载后打开sdk-mana ...
- js原型链接(二)和object类的create方法
原型链的内部执行方式 <script> function Myclass(){ this.x=" x in Myclass"; } var obj=new Myclas ...
- php 执行事务的时候pdo出现问题
新版本的pdo会有这个问题: General error: 2014 Cannot execute queries while other unbuffered queries are active. ...