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 ...
随机推荐
- Solaris进程管理
ps-a 列出与终端有关的进程-e 列出所有进程-A 同-e-f 列出进程完整信息-l 生成一个长列表-u username 列出某用户的进程 常用:ps -ef ...
- activiti搭建(四)八项服务介绍
转载请注明源地址:http://www.cnblogs.com/lighten/p/5927949.html 1.前言 之前学习的时候一直在其它文章看到activiti提供了七个接口来操作工作流,但在 ...
- 简单linux字符设备驱动程序
本文代码参考<LINUX设备驱动程序>第三章 字符设备驱动程序 本文中的“字符设备”是一段大小为PAGE_SIZE的内存空间 功能:向字符设备写入字符串:从字符设备读出字符串 代码: 1. ...
- mysql存储引擎(mysql学习六)
存储引擎 现在只有InnoDB支持外键 上接着学习笔记五 class表中有外键,所以不能修改存储引擎 表类型 默认的服务器表类型,通过my.ini可以配置 Default-storage-e ...
- php根据日期获得星期
<?php $weekarray=array("日","一","二","三","四",&quo ...
- 【easuyi】---easyui中的验证validatebox自定义
这里比较简单的使用就不再多说,主要说一下自定义的validatebox. 1.验证密码是否相等,这个直接参考给定的列子就行,这里主要学习这种灵活使用的方式和方法. <input id=" ...
- STM32F0xx_FLASH编程(片内)配置详细过程
Ⅰ.概述 关于数据的储存,我觉得编程的人基本上都会使用到,只是看你储存在哪里.STM32的芯片内部FLASH都是可以进行编程的,也就是说可以拿来储存数据.但是,很多做一些小应用程序开发的人都没有利用好 ...
- linuxok6410的I2C驱动分析---用户态驱动
3 i2c-dev 3.1 概述 之前在介绍I2C子系统时,提到过使用i2c-dev.c文件在应用程序中实现我们的I2C从设备驱动.不过,它实现的是一个虚拟,临时的i2c_client,随着设备文件 ...
- 在MAC OS 下配置python + Flask ,并支持pyCharm编辑器
原创咯- flask是一个micro framework ,伸缩性很强.可以部署到openshift 的PAAS里.这个框架上手非常快.喜欢的可以试试. 若实在MAC里,python已经默认安装了.1 ...
- WPF自定义控件(一)——Button
接触WPF也有两个多月了,有了一定的理论基础和项目经验,现在打算写一个系列,做出来一个WPF的控件库.一方面可以加强自己的水平,另一方面可以给正在学习WPF的同行一个参考.本人水平有限,难免有一些错误 ...