Backpack |

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

Example

If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select[2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

分析:

看似这题是NP-hard问题,但是实际上可以用DP解决。result[i][j] 表示选取数组A中前i个数并且backpack size 是 j的时候,backpack剩余的size最小。

result[i][j] = Math.min(result[i - 1][j], result[i - 1][j - A[i]]);

 public class Solution {

     public int backPack(int m, int[] A) {
if (A == null || A.length == || m <= ) return m; int[][] result = new int[A.length][m + ];
for (int i = ; i < result.length; i++) {
for (int j = ; j <= m; j++) {
if (i == ) {
if (A[i] > j) {
result[i][j] = j;
} else {
result[i][j] = j - A[i];
}
} else {
if (A[i] > j) {
result[i][j] = result[i - ][j];
} else {
result[i][j] = Math.min(result[i - ][j], result[i - ][j - A[i]]);
}
} }
}
return m - result[A.length - ][m];
}
}

Backpack II

Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value can you put into the backpack?

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

分析:

原理同上,转移方程如下:

maxValue[i][j] = Math.max(maxValue[i - 1][j], maxValue[i - 1][j - A[i]] + V[i]);

 public class Solution {
public int backPackII(int m, int[] A, int V[]) {
if (m <= || A == null || A.length == || V == null || V.length == ) return ; int[][] maxValue = new int[A.length][m + ]; for (int i = ; i < maxValue.length; i++) {
for (int j = ; j < maxValue[].length; j++) {
if ( i == ) {
if (A[i] <= j) {
maxValue[i][j] = V[i];
}
} else {
if (A[i] <= j) {
maxValue[i][j] = Math.max(maxValue[i - ][j], maxValue[i - ][j - A[i]] + V[i]);
} else {
maxValue[i][j] = maxValue[i - ][j];
}
}
}
}
return maxValue[maxValue.length - ][maxValue[].length - ];
}
}

参考请注明出处:cnblogs.com/beiyeqingteng/

Backpack | & ||的更多相关文章

  1. [LintCode] Backpack VI 背包之六

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  2. LintCode "Backpack"

    A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...

  3. LeetCode Backpack

    Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...

  4. Backpack III

    Description Given n kinds of items, and each kind of item has an infinite number available. The i-th ...

  5. Backpack IV

    Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate ...

  6. Backpack V

    Description Given n items with size nums[i] which an integer array and all positive numbers. An inte ...

  7. Backpack II

    Description There are n items and a backpack with size m. Given array A representing the size of eac ...

  8. Backpack VI

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  9. 0-1背包问题蛮力法求解(java版本)

    sloves: package BackPack; public class Solves {  public int[] DecimaltoBinary(int n,int m)  {   int ...

随机推荐

  1. Address already in use: JVM_Bind<null>:80

    Address already in use: JVM_Bind<null>:80 咱还是闲话少说,直接切入正题. 起因: 一直用Tomcat,但是前几天突然报错: java.net.Bi ...

  2. Java 线程并发策略

    1 什么是并发问题. 多个进程或线程同时(或着说在同一段时间内)访问同一资源会产生并发问题. 2 java中synchronized的用法 用法1 public class Test{ public ...

  3. 模式匹配KMP算法

    关于KMP算法的原理网上有很详细的解释,我试着总结理解一下: KMP算法是什么 以这张图片为例子 匹配到j=5时失效了,BF算法里我们会使i=1,j=0,再看s的第i位开始能不能匹配,而KMP算法接下 ...

  4. Day5_作业

     ATM作业:1.额度15000或自定义2.实现购物商城,买东西加入购物车,调用信用卡接口结账3.可以提现,手续费5%4.每月22号出账单,每月10号为还款日,过期未还,按欠款总额万分之5每日计息5. ...

  5. Oracle 调度程序(scheduler)摘自一位大神

    在11g中,Oracle提供了一个新建的Scheduler特性,帮助将作业实现自动化.它还可以帮助你控制资源的利用与并可以将数据库中的作业按优先顺序执行.传统的dbms_jobs的一个限制是它只能调度 ...

  6. easyui form submit 不提交

    http://bbs.csdn.net/topics/390811964 function saveProduct() {             //$('#fm').form('submit',  ...

  7. ASP.NET MVC4中调用WEB API的四个方法

    http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...

  8. ExtJS入门教程03,form中怎能没有validation

    接上篇内容,我们在学会extjs form的基本用法之后,今天我们来看看extjs form的validation功能. 必填项,就是不能为空(allowBlank) 效果: 代码: { xtype: ...

  9. ci中如何得到配置的url

    $this->load->helper('url'); 然后,你可以用它查询并返回设置在config.php文件中的site和/或base URL: echo site_url(); ec ...

  10. java中获取路径中的空格处理(%20)问题

    在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...