Coin Change 2
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
Note:You can assume that
0 <= amount <= 5000
1 <= coin <= 5000
the number of coins is less than 500
the answer is guaranteed to fit into signed 32-bit integer
Thoughts:
dp[i][j]: the number of combinations to make up amountjby using the firstitypes of coinsState transition:not using the
ith coin, only using the firsti-1coins to make up amountj, then we havedp[i-1][j]ways.
using the
ith coin, since we can use unlimited same coin, we need to know how many way to make up amountj - coins[i]by using firsticoins( includingith), which isdp[i][j-coins[i]]Initialization:dp[i][0] = 1
dp[i][j]only rely ondp[i-1][j]anddp[i][j-coins[i]], then we can optimize the space by only using one-dimension array.
Code: 2D DP: O(K * N), S: O(K * N)
class Solution {
public int change(int amount, int[] coins) {
int[][] dp = new int[coins.length+1][amount+1];
dp[0][0] = 1;
for (int i = 1; i <= coins.length; i++) {
dp[i][0] = 1;
for (int j = 1; j <= amount; j++) {
dp[i][j] = dp[i-1][j] + (j >= coins[i-1] ? dp[i][j-coins[i-1]] : 0);
}
}
return dp[coins.length][amount];
}
}Code: 1D DP: O(K * N), S: O(N)
Last updated
Was this helpful?