dsa · medium
Coin Change
GreyOrangeDynamic Programming
You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return 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.
Arguments
coins— available coin denominationsamount— the target amount to make
**Example 1:** `` Input: coins = [1,2,5], amount = 11 Output: 3 ``
Constraints
1 <= coins.length <= 12; 0 <= amount <= 4*10^4 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,5] 11 Expected: 3
Example 2
Input: [2] 3 Expected: -1