-
Notifications
You must be signed in to change notification settings - Fork 1
/
Knapsack.java
41 lines (34 loc) · 1.25 KB
/
Knapsack.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
36
37
38
39
40
41
import java.util.*;
class Knapsack {
static int solveKnapsack(int[] profits, int[] weights, int capacity) {
if(capacity <= 0 || profits.length == 0 || weights.length != profits.length) return 0;
int n = profits.length;
int[] dp = new int[capacity + 1];
for(int c = 0; c <= capacity; c++) {
if(weights[0] <= c) {
dp[c] = profits[0];
}
}
for(int i = 1; i < n; i++) {
for(int c = capacity; c>=0; c--) {
int profit1 = 0, profit2 = 0;
if(weights[i] <= c) {
profit1 = profits[i] + dp[c-weights[i]];
} else {
profit2=dp[c];
}
dp[c] = Math.max(profit1,profit2);
}
}
return dp[capacity];
}
public static void main(String[] args) {
Knapsack ks = new Knapsack();
int[] profits = {1, 6, 10, 16};
int[] weights = {1, 2, 3, 5};
int maxProfit = ks.solveKnapsack(profits, weights, 7);
System.out.println("Total knapsack profit ---> " + maxProfit);
maxProfit = ks.solveKnapsack(profits, weights, 6);
System.out.println("Total knapsack profit ---> " + maxProfit);
}
}