Skip to content

Commit 394011f

Browse files
committed
Knapsack 0-1
1 parent 9c50e2f commit 394011f

File tree

1 file changed

+3
-3
lines changed
  • datastructure-algorithm-java-examples/src/main/java/com/hellokoding/algorithm

1 file changed

+3
-3
lines changed

datastructure-algorithm-java-examples/src/main/java/com/hellokoding/algorithm/DP_Knapsack.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ int findMaxValueOfKnapSack(int[] values, int[] weights, int W, int N) {
99
if (i == 0 || w == 0) {
1010
cache[i][w] = 0;
1111
}
12-
else if (weights[i-1] <= w) {
13-
cache[i][w] = Math.max(cache[i-1][w], cache[i-1][w-weights[i-1]] + values[i-1]);
14-
} else {
12+
else if (weights[i-1] > w) {
1513
cache[i][w] = cache[i-1][w];
14+
} else {
15+
cache[i][w] = Math.max(cache[i-1][w], cache[i-1][w-weights[i-1]] + values[i-1]);
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)