Blind 75
55 / 75Free practice

Coin Change

Medium · Dynamic programming

Jump to code

Given positive coin denominations and a nonnegative amount, find the fewest coins needed to make that amount with unlimited copies. Return −1 when it is impossible.

Examples

Example 1

coins = [1,2,5]
amount = 11

Output: 3

Example 2

coins = [2]
amount = 3

Output: -1

Constraints

  • 1 <= coins.length <= 10
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10000
Need a hint?

The best solution for a total can extend a smaller reachable total by one coin.

Asked in real interviews

Candidates reported versions of Coin Change from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    1,
    2,
    5
  ],
  11
]
Expected
3
Case 2
Input
[
  [
    2
  ],
  3
]
Expected
-1
Case 3
Input
[
  [
    1
  ],
  0
]
Expected
0
Case 4
Input
[
  [
    2,
    5,
    10
  ],
  6
]
Expected
3
Case 5
Input
[
  [
    1,
    3,
    4
  ],
  6
]
Expected
2

Drafts are saved per language in this browser when storage is available.