Coding problem · Medium · Dynamic Programming

Coin Change

Medium Algorithms & data structures Most askedSeen at Amazon, Google, Goldman Sachs, Bloomberg

Given coin denominations coins (unlimited supply of each) and an amount, return the fewest number of coins that make up that amount, or -1 if it can't be made.

Example 1

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

Example 2

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

Constraints

  • 0 <= amount <= 10^4

Hints

Hint 1

Greedy (largest coin first) fails for coins like [1,3,4] and amount 6.

Hint 2

dp[a] = 1 + min(dp[a − c]) over coins c ≤ a.

Write and run your solution

Loading the editor… (JavaScript is required to run code in your browser.)

Solution and explanation

Show the solution

Time O(amount × coins) · Space O(amount)

Bottom-up DP: dp[0] = 0, every other amount starts at ∞. For each amount, try every coin and keep the minimum dp[a - c] + 1. If dp[amount] is still ∞, it's unreachable.

Reference solution

function coinChange(coins, amount) {
  const dp = new Array(amount + 1).fill(Infinity);
  dp[0] = 0;
  for (let a = 1; a <= amount; a++)
    for (const c of coins) if (c <= a) dp[a] = Math.min(dp[a], dp[a - c] + 1);
  return dp[amount] === Infinity ? -1 : dp[amount];
}

Common mistakes

  • Greedy coin choice — fails on non-canonical coin systems.