Backpack | & ||

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]]);

 1 public class Solution {
 2 
 3     public int backPack(int m, int[] A) {
 4         if (A == null || A.length == 0 || m <= 0) return m;
 5         
 6         int[][] result = new int[A.length][m + 1];
 7         for (int i = 0; i < result.length; i++) {
 8             for (int j = 0; j <= m; j++) {
 9                 if (i == 0) {
10                     if (A[i] > j) {
11                         result[i][j] = j;
12                     } else {
13                         result[i][j] = j - A[i];
14                     }
15                 } else {
16                     if (A[i] > j) {
17                         result[i][j] = result[i - 1][j];
18                     } else {
19                         result[i][j] = Math.min(result[i - 1][j], result[i - 1][j - A[i]]);
20                     }
21                 }
22                 
23             }
24         }
25         return m - result[A.length - 1][m];
26     }
27 }

 

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]);

 1 public class Solution {
 7     public int backPackII(int m, int[] A, int V[]) {
 8         if (m <= 0 || A == null || A.length == 0 || V == null || V.length == 0) return 0;
 9         
10         int[][] maxValue = new int[A.length][m + 1];
11         
12         for (int i = 0; i < maxValue.length; i++) {
13             for (int j = 0; j < maxValue[0].length; j++) {
14                 if ( i == 0) {
15                     if (A[i] <= j) {
16                         maxValue[i][j] = V[i];
17                     }
18                 } else {
19                     if (A[i] <= j) {
20                         maxValue[i][j] = Math.max(maxValue[i - 1][j], maxValue[i - 1][j - A[i]] + V[i]);
21                     } else {
22                         maxValue[i][j] = maxValue[i - 1][j];
23                     }
24                 }
25             }
26         }
27         return maxValue[maxValue.length - 1][maxValue[0].length - 1];
28     }
29 }

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

转载于:https://www.cnblogs.com/beiyeqingteng/p/5642218.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值