Problem · Array
Coin Change
Learn this problemProblem statement
You are given an array coins of distinct positive coin denominations and a nonnegative integer amount.
You have an unlimited supply of every denomination. Return the minimum number of coins needed to make exactly amount. If no combination can make the amount, return -1.
An amount of 0 requires 0 coins.
Function
coinChange(coins: int[], amount: int) → intExamples
Example 1
coins = [1,2,5]amount = 11return = 3Two coins of denomination 5 and one coin of denomination 1 make 11 with three coins.
Example 2
coins = [2]amount = 3return = -1No number of coins with denomination 2 can make the odd amount 3.
Example 3
coins = [1]amount = 0return = 0The empty selection makes amount 0.
Constraints
1 <= coins.length <= 12.1 <= coins[i] <= 2^31 - 1.- All denominations are distinct.
0 <= amount <= 10000.