Skip to main content

Coin Change

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Problems38 / 152
Sign in

Coin Change

MEDIUMArrayDynamic Programming

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1

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

Example 2

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

Use DP: dp[i] = minimum coins needed for amount i. For each coin, update dp[i] = min(dp[i], dp[i-coin] + 1) if i >= coin.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
coins =
[1,2,5]
amount =
11
Expected
3
Run with your own input (JSON)