forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroOneKnapsack.java
More file actions
38 lines (35 loc) · 1.46 KB
/
ZeroOneKnapsack.java
File metadata and controls
38 lines (35 loc) · 1.46 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
package com.thealgorithms.dynamicprogramming;
/**
* The {@code ZeroOneKnapsack} class provides a method to solve the classic 0/1 Knapsack problem.
* It returns the maximum value that can be obtained by selecting items within the weight limit.
*
* Problem Description: Given weights and values of n items, put these items in a knapsack of capacity W
* such that the total value is maximized. You cannot break an item, either pick the complete item or don't pick it.
*
* https://en.wikipedia.org/wiki/Knapsack_problem
*/
public final class ZeroOneKnapsack {
private ZeroOneKnapsack() {
}
/**
* Solves the 0/1 Knapsack problem using recursion.
*
* @param values the array containing values of the items
* @param weights the array containing weights of the items
* @param capacity the total capacity of the knapsack
* @param n the number of items
* @return the maximum total value achievable within the given weight limit
*/
public static int compute(int[] values, int[] weights, int capacity, int n) {
if (n == 0 || capacity == 0) {
return 0;
}
if (weights[n - 1] <= capacity) {
int include = values[n - 1] + compute(values, weights, capacity - weights[n - 1], n - 1);
int exclude = compute(values, weights, capacity, n - 1);
return Math.max(include, exclude);
} else {
return compute(values, weights, capacity, n - 1);
}
}
}