Given an integer array coins
indicating different coin denominations and an integer target
denoting a total sum of money, return the minimum number of coins needed to make up the target
. If it's not possible to make up the target
with any combination of the coins, return -1
.
Assume there's an infinite supply of each coin.
coins: number[]
: An array of integerstarget: number
: An integerInput: coins = [3,7,4], target = 14Output: 2Explanation: We can form the target amount (14) using the following combination of coins: 7 + 7. This requires a minimum of 2 coins.
Input: coins = [1], target = 0Output: 0Explanation: The target amount is 0, which requires no coins to reach. Even though there's only a 1 coin available, you can still form 0 using no coins.
Input: coins = [2], target = 3Output: -1Explanation: The only available coin denomination is 2. However, it's impossible to form the target amount (3) using just multiples of 2. Therefore, no combination of coins can reach 3, and the output is -1.
coins.length
<= 12coins[i]
<= 1,000,000target
<= 10,000Given an integer array coins
indicating different coin denominations and an integer target
denoting a total sum of money, return the minimum number of coins needed to make up the target
. If it's not possible to make up the target
with any combination of the coins, return -1
.
Assume there's an infinite supply of each coin.
coins: number[]
: An array of integerstarget: number
: An integerInput: coins = [3,7,4], target = 14Output: 2Explanation: We can form the target amount (14) using the following combination of coins: 7 + 7. This requires a minimum of 2 coins.
Input: coins = [1], target = 0Output: 0Explanation: The target amount is 0, which requires no coins to reach. Even though there's only a 1 coin available, you can still form 0 using no coins.
Input: coins = [2], target = 3Output: -1Explanation: The only available coin denomination is 2. However, it's impossible to form the target amount (3) using just multiples of 2. Therefore, no combination of coins can reach 3, and the output is -1.
coins.length
<= 12coins[i]
<= 1,000,000target
<= 10,000console.log()
statements will appear here.