-
Notifications
You must be signed in to change notification settings - Fork 35
/
UnboundedKnapsack.java
35 lines (32 loc) · 1013 Bytes
/
UnboundedKnapsack.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
/**
Problem: https://en.wikipedia.org/wiki/Knapsack_problem#Unbounded_knapsack_problem
*/
public class UnboundedKnapsack {
public static void main(String[] args) {
int C = 17;
int[] wt = {4, 3, 5, 7, 11};
int[] val = {5, 3, 6, 2, 7};
int N = wt.length;
System.out.println("Max Profit: " + unboundedKnapsack(C, N, wt, val));
}
/**
*
* @param C: total weight
* @param N: N items
* @param wt: weights
* @param val: values
* @return: maximum value of items whose weight does not exceed C
*/
public static int unboundedKnapsack(int C, int N, int[] wt, int[] val){
// dp[i]: the current "best "solution" (max value) for cap. of i
int[] dp = new int[C + 1];
for (int i = 0; i <= C; i ++){
for (int j = 0; j < N; j ++){
if(i >= wt[j]){
dp[i] = Math.max(dp[i], val[j] + dp[i - wt[j]]);
}
}
}
return dp[C];
}
}