Skip to content

Coin Change

MediumDynamic Programming
Decode it first. Clues, constraints, pattern and plan before any code.

You have unlimited coins of each value in coins. Return the fewest coins that add up to exactly amount, or -1 if no combination works.

coins = [1, 2, 5], amount = 11  →  3    (5 + 5 + 1)
coins = [2], amount = 3         →  -1
coins = [1], amount = 0         →  0

Constraints

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10^4