Created
August 28, 2020 20:53
-
-
Save bparanj/b4bce3d3e444e9267653f64cf3cfbc2e to your computer and use it in GitHub Desktop.
This file contains 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 Solution { | |
public int change(int amount, int[] coins) { | |
int[][] dp = new int[coins.length + 1][amount + 1]; | |
dp[0][0] = 1; | |
for (int j = 1; j <= coins.length; j++) { | |
dp[j][0] = 1; // number ways of selecting for amount zero is 1 | |
for (int i = 1; i <= amount; i++) { | |
dp[j][i] = dp[j - 1][i]; // exclude current coin | |
if (i - coins[j - 1] >= 0) { // check if amount >= to the current i`th coin | |
dp[j][i] += dp[j][i - coins[j - 1]]; // include current coin | |
} | |
} | |
} | |
return dp[coins.length][amount]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment