Skip to interview prep
Apexed

40. Coin Change

MediumDynamic programming

Given coin denominations coins (unlimited supply) and an amount, return the fewest coins that make up the amount, or -1 if it cannot 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

  • 1 ≤ coins.length ≤ 12
  • 0 ≤ amount ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Coin Change — coding interview practice — Apexed