322 Coin Change
322. Coin Change
1. Question
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.
2. Implementation
(1) DP
思路: dp[i]表示数量为i所需要的coin个数,初始化dp[0] = 0, 状态方程为dp[i] = Math.min(dp[i], dp[i - coins[j]]), where i >= coins[j]
3. Time & Space Complexity
时间复杂度O(n * amount), 空间复杂度O(amount)
Last updated
Was this helpful?