322 Coin Change
Last updated
Was this helpful?
Last updated
Was this helpful?
You are given coins of different denominations and a total amount of moneyamount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return-1
.
Example 1:
Example 2:
Note: You may assume that you have an infinite number of each kind of coin.
(1) DP
思路: dp[i]表示数量为i所需要的coin个数,初始化dp[0] = 0, 状态方程为dp[i] = Math.min(dp[i], dp[i - coins[j]]), where i >= coins[j]
时间复杂度O(n * amount), 空间复杂度O(amount)