-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathKnapsack.java
More file actions
38 lines (31 loc) · 1.2 KB
/
Knapsack.java
File metadata and controls
38 lines (31 loc) · 1.2 KB
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
// Knapsack Problem using Dynamic Programming
public class Knapsack {
public static int knapsack(int capacity, int[] weights, int[] values, int n) {
// Create a 2D array to store the maximum value at each n and weight combination
int[][] dp = new int[n + 1][capacity + 1];
// Build the dp array in bottom-up manner
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= capacity; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
}
else if (weights[i - 1] <= w) {
dp[i][w] = Math.max(dp[i - 1][w], values[i - 1] + dp[i - 1][w - weights[i - 1]]);
}
else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][capacity];
}
public static void main(String[] args) {
int[] weights = {1, 3, 4, 5};
int[] values = {1, 4, 5, 7};
int capacity = 7;
int n = weights.length;
// Function call
int maxValue = knapsack(capacity, weights, values, n);
System.out.println("Maximum value in Knapsack = " + maxValue);
}
}