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.
30:00
Java
Coin Change
medium
Topics
Companies
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
Constraints:
1≤coins.length≤12
1≤coins[i]≤231−1
0≤amount≤104
Input
arr ={"coins":[1,2,5],"amount":11}
Init DP array with max values
Coins
1
2
5
DP (Min Coins for Amount)
0
0
∞
1
∞
2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
∞
9
∞
10
∞
11
Variables
Variable
Value
amount
11
dp[0]
0
DepthFunction Call
Stack empty
0/95
Asked in these interview guides
Company guides that list this problem among their sample questions.