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 ...
随机推荐
- SAS 5/iR Adapter 驱动下载
http://www.dell.com/support/home/cn/zh/cnbsd1/Drivers/DriversDetails?driverId=FF6F6
- 内核linux-3.4.2支持dm9000
当前烧写: fs: nfs 30000000 192.168.1.17:/work/nfs_root/first_fs_mdev.yaffs2 //这里不能使用nfs挂载,只能直 ...
- 个人博客实现Archives查询小记
这两天正在做博客,刚刚遇到一个问题,就是需要在主页实现文档分类功能,即通过日期将文章进行按日期进行分类. 比如这样的: 我个人的想法是,查询所有文章的日期,然后将日期进行格式化,只留下年份和月份,然后 ...
- 小菜的系统框架界面设计-灰姑娘到白雪公主的蜕变(工具条OutLookBar)
灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌 --蜕变--> 我这里讲的是一个工具条的蜕变过程, ...
- LVS+keepalived实现负载均衡
背景: 随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要 的浪费?同时实现商业硬件一样的 ...
- ContactsContract.CommonDataKinds【Translated By KillerLegend】
http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.html interf ...
- emlog模板制作帮助手册
emlog基本模板文件 必须文件: main.css: CSS(样式表)文件 log_list.php: 日志列表页模板 echo_log.php: 日志页模板 page.php: 页面(page)模 ...
- 添加常驻Notification
private static final int NOTIFICATION_ID=250; //用来标示notification,通过notificatinomanager来发布同样标示的notifi ...
- 如何让dapper支持oracle游标呢?
Dapper是一个轻型的ORM类.它有啥优点.缺点相信很多朋友都知道了,园里也有很多朋友都有相关介绍,这里就不多废话. 如果玩过Oracle都知道,存储过程基本都是通过游标返回数据的,但是dapper ...
- WIN8+VS2013编写发布WCF之二(部署)
上文简介了如何建立WCF工程并且调试,下面说一下如何部署. 本文将陆陆续续讲述三种部署方式,随着项目的进展将不断补全. 声明: 用管理员身份打开VS2013,发布前请将程序的.net版本改成与服务器相 ...