P14 · 2 seconds · 256 MB
Coin Change
You have an unlimited supply of coins with denominations `coins[i]`. Return the fewest coins that sum to exactly `amount`. If it is impossible, return `-1`. Order does not matter. Making amount `0` takes `0` coins.
function coinChange(coins, amount) → number
CONSTRAINTS
- 1 ≤ coins.length ≤ 12
- 1 ≤ coins[i] ≤ 10⁴
- 0 ≤ amount ≤ 10⁴
Input: coins = [1, 2, 5], amount = 11
Output: 3
5 + 5 + 1.
Input: coins = [2], amount = 3
Output: -1
Input: coins = [1], amount = 0
Output: 0
1
2
3
4
5
6
7
8
Arena judges JavaScript in a Web Worker (1s wall-clock, then TLE). Samples are public; submit runs hidden tests too. C++/Python is a later swarm package.