ntree.ai
ntree.aiCoin Change

Coin Change

mediumsolvedattempted

You are given an array of integers coins holding the coin values available, and an integer amount. You have an unlimited number of each coin.

Return the fewest coins that add up to amount, or -1 if no combination of them does.

Example 1:

Input:  coins = [1, 3, 4], amount = 5
Output: 2
Explanation: 4 + 1 = 5.

Example 2:

Input:  coins = [2], amount = 3
Output: -1

Constraints:

  • 1 <= length of coins <= 12
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10^4
def coinChange(coins: list[int], amount: int) -> int:
pass
 

Press Visualise to step through your code.