Backpack | & ||

Posted 北叶青藤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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/

以上是关于Backpack | & ||的主要内容,如果未能解决你的问题,请参考以下文章

背包型动态规划

Laravel Backpack - 内联创建,未在数据库中添加关系

对这个带有 & 不带 = 的代码片段返回类型感到非常困惑

设置中的 Laravel-Backpack/Settings 表字段

在 MyAccountController [backpack-laravel] 中添加个人资料信息

Sphinx - 在代码块片段中使用省略号 (...)