Created
October 17, 2017 17:10
-
-
Save aaronjwood/582a0c6f45535ec8d92d611b0d55c240 to your computer and use it in GitHub Desktop.
Ways to make change (naive approach and dynamic programming approach)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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