forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19_backPackII.java
35 lines (34 loc) · 974 Bytes
/
19_backPackII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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[]) {
// write your code here
if(0 == m || A == null || V == null || 0 == A.length)
return 0;
int len = A.length;
int [][] val = new int [len][m+1];
for(int i = 0; i < len; i++){
val[i][0] = 0;
}
for(int i = 0; i < m+1; i++){
if(i >= A[0])
val[0][i] = V[0];
}
for(int i = 1; i < len; i++){
for(int j = 1; j < m+1; j++){
if(j >= A[i]){
val[i][j] = max(val[i-1][j],val[i-1][j-A[i]]+V[i]);
}else{
val[i][j] = val[i-1][j];
}
}
}
return val[len-1][m];
}
public int max(int a, int b) {
return a > b ? a : b;
}
}