LintCode刷题--背包问题 II
来源:互联网 发布:e订通软件 编辑:程序博客网 时间:2024/05/16 11:00
问题描述:
给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大?
样例:
对于物品体积[2, 3, 5, 7]和对应的价值[1, 5, 2, 4], 假设背包大小为10的话,最大能够装入的价值为9。
public class Solution { /* * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @param V: Given n items with value V[i] * @return: The maximum value */ public int backPackII(int m, int[] array, int[] value) { int [][]dp=new int[array.length][m+1]; for(int i=0;i<array.length;i++){ dp[i][0]=0; } for(int j=0;j<m+1;j++){ if(j>=array[0]){ dp[0][j]=value[0]; } } for(int i=1;i<array.length;i++){ for(int j=1;j<m+1;j++){ if(j>=array[i]){ dp[i][j]=Integer.max(dp[i-1][j],dp[i-1][j-array[i]]+value[i]); }else{ dp[i][j]=dp[i-1][j]; } } } return dp[array.length-1][m]; }}
阅读全文
0 0
- LintCode刷题--背包问题 II
- LintCode-背包问题 II
- LintCode : 背包问题 II
- LintCode : 背包问题 II
- LintCode:背包问题 II
- lintcode--背包问题II
- lintcode-背包问题II-125
- LintCode:M-背包问题 II
- LintCode 125. 背包问题 II
- 【LintCode】 Backpack II 背包问题II
- LintCode Backpack II(背包II)
- LintCode刷题——背包问题
- 【LintCode】Backpack 背包问题
- lintcode-背包问题-92
- LintCode :背包问题
- lintcode backpack 背包问题
- LintCode:背包问题
- LintCode(M)背包问题
- 简单的学生成绩的输出
- spring-mvc.xml
- linux 打印级别控制
- Android Arch Comp
- MONKEY
- LintCode刷题--背包问题 II
- 高通平台LCM相关专业术语首字母缩写Acronyms(不断更新。。。)
- 自我参考-Java面向对象(下)
- python中对类的理解
- 类和对象
- 【正念奇迹】-ZQ0014-《高绩效教练》
- Judge Route Circle
- Java使用线程池批量顺序执行任务
- 太上感应篇0021