Skip to content

Instantly share code, notes, and snippets.

@aaronjwood
Created October 17, 2017 17:10
Show Gist options
  • Save aaronjwood/582a0c6f45535ec8d92d611b0d55c240 to your computer and use it in GitHub Desktop.
Save aaronjwood/582a0c6f45535ec8d92d611b0d55c240 to your computer and use it in GitHub Desktop.
Ways to make change (naive approach and dynamic programming approach)
class Change {
static int countDP(int coins[], int cents) {
int[] table = new int[cents + 1];
table[0] = 1;
for (int coin : coins) {
for (int j = coin; j <= cents; j++) {
table[j] += table[j - coin];
}
}
return table[cents];
}
static int countNaive(int S[], int m, int n) {
if (n == 0) {
return 1;
}
if (n < 0 || m <= 0) {
return 0;
}
return countNaive(S, m - 1, n) + countNaive(S, m, n - S[m - 1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment