Coin Change
Problem statement
You are given an integer array coins containing distinct positive denominations and a nonnegative integer amount.
You may use each denomination any number of times. Return the minimum number of coins needed to make exactly amount. If no combination can make the amount, return -1.
Function
coinChange(coins: int[], amount: int) → intExamples
Example 1
coins = [1,2,5]amount = 11return = 3The amount can be formed with 5 + 5 + 1.
Example 2
coins = [2]amount = 3return = -1No number of 2-value coins can form 3.
Example 3
coins = [2,3,7]amount = 0return = 0No coins are needed to form amount 0.
Constraints
1 <= coins.length <= 12.1 <= coins[i] <= 2^31 - 1.- All denominations are distinct.
0 <= amount <= 10^4.