-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThe0And1Knapsack.java
More file actions
57 lines (51 loc) · 1.88 KB
/
The0And1Knapsack.java
File metadata and controls
57 lines (51 loc) · 1.88 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package algorithm.DynamicProgramming;
/**
* The type The 0 and 1 knapsack.
*/
public class The0And1Knapsack {
/**
* Gets knapp sck.
*
* @param weight the weight
* @param profit the profit
* @param capacity the capacity
* @return the knapp sck
*/
public static int getKnappSck(int[] weight, int[] profit, int capacity) {
int[][] dp = new int[profit.length][capacity + 1];
return getKnappSckRecursive(dp, weight, profit, capacity, 0);
}
/**
* Gets knapp sck recursive.
*
* @param dp the dp
* @param weight the weight
* @param profit the profit
* @param capacity the capacity
* @param currentIndex the current index
* @return the knapp sck recursive
*/
private static int getKnappSckRecursive(int[][] dp, int[] weight, int[] profit, int capacity, int currentIndex) {
if (capacity <= 0 || currentIndex >= profit.length || currentIndex < 0 || weight.length != profit.length)
return 0;
if (dp[currentIndex][capacity] != 0)
return dp[currentIndex][capacity];
int profit0 = 0;
if (weight[currentIndex] <= capacity) {
profit0 = profit[currentIndex] + getKnappSckRecursive(dp, weight, profit, capacity - weight[currentIndex], currentIndex + 1);
}
int profit1 = getKnappSckRecursive(dp, weight, profit, capacity, currentIndex + 1);
dp[currentIndex][capacity] = Math.max(profit1, profit0);
return dp[currentIndex][capacity];
}
/**
* Main.
*
* @param args the args
*/
public static void main(String args[]) {
int profits[] = {1, 6, 10, 16}; // The values of the jewelry
int weights[] = {1, 2, 3, 5}; // The weight of each
System.out.println("Maximum profit get from KnapSack :: " + getKnappSck(weights, profits, 6));
}
}