Skip to content

Commit dccaefc

Browse files
committed
Coin change
1 parent 443a283 commit dccaefc

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hellokoding.algorithm;
22

3+
import java.util.Arrays;
4+
35
public class DP_CoinChange {
46
static int countWays(int[] coins, int targetCoinChange) {
57
int[] wayOfCoinChanges = new int[targetCoinChange+1];
@@ -10,12 +12,29 @@ static int countWays(int[] coins, int targetCoinChange) {
1012
for (int j = coins[i]; j <= targetCoinChange; j++) {
1113
wayOfCoinChanges[j] += wayOfCoinChanges[j - coins[i]];
1214
}
15+
System.out.println(Arrays.toString(wayOfCoinChanges));
1316
}
1417

1518
return wayOfCoinChanges[targetCoinChange];
1619
}
1720

1821
public static void main(String[] args) {
1922
System.out.println(countWays(new int[]{2, 3, 1}, 4));
23+
24+
/*
25+
* w[0] = 1
26+
* i=0, j=2, w[2] = 0 + w[2-2] = 0 + w[0] = 1
27+
* w[3] = w[3] + w[3-2] = w[3] + w[1] = 0 + 0 = 0
28+
* w[4] = w[4] + w[4-2] = 0 + w[2] = 1
29+
*
30+
* i=1, j=3, w[3] = w[3] + w[3-3] = 1
31+
* w[4] = w[4] + w[1] = 1
32+
*
33+
* i=2, j=1, w[1] = w[1] + w[0] = 1
34+
* j=2 w[2] = w[2] + w[1] = 2
35+
* j=3 w[3] = w[3] + w[2] = 3
36+
* j=4 w[4] = w[4] + w[3] = 4
37+
*
38+
* */
2039
}
2140
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
public class DP_UniquePaths_Obstacles {
44
int countUniquePaths(int[][] A) {
5+
if (A[0][0] == 1) return 0;
6+
57
int rows = A.length;
68
int cols = A[0].length;
7-
89
int[][] cache = new int[rows][cols];
910

10-
if (A[0][0] == 0)
11-
cache[0][0] = 1;
11+
cache[0][0] = 1;
1212

1313
for (int i = 1; i < rows; i++) {
1414
if (A[i][0] == 0)

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)