Coin Change

Medium

You are given an array coins of coin values and a target amount. You have an unlimited supply of every coin.

Return the fewest coins that add up to exactly amount. If no combination of coins adds up to amount, return -1.

Examples

Example 1

Input:  coins = [1,2,5], amount = 11
Output: 3
Example 1: amount = 11551exactly 113 coins

11 = 5 + 5 + 1.

Example 2

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

Any number of 2s adds up to an even total, so 3 can't be made.

Example 3

Input:  coins = [1], amount = 0
Output: 0

An amount of 0 needs no coins at all.

Example 4

Input:  coins = [1,3,4], amount = 6
Output: 2
Example 4: amount = 6Fewest coins332 coinsBiggest coin first4113 coins

6 = 3 + 3. Taking the biggest coin first gives 4 + 1 + 1, which is one coin too many.

Example 5

Input:  coins = [7,5], amount = 24
Output: 4
Example 5: amount = 247 + 7 + 5 + 577554 coinsBiggest coin first: 7 + 7 + 77773 left,no coin fits

24 = 7 + 7 + 5 + 5. Taking the biggest coin first uses three 7s, reaches 21, and then gets stuck with 3 left, even though the amount can be made.

Constraints

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 2^31 - 1
  • 0 <= amount <= 10^4
TypeScript
Loading editor…
Test cases45
Case 1
coins = [1,2,5]
amount = 11
Case 2
coins = [2]
amount = 3
Case 3
coins = [1]
amount = 0
Case 4
coins = [1,3,4]
amount = 6
Case 5
coins = [7,5]
amount = 24
Case 6
coins = [186,419,83,408]
amount = 6249
Case 7
coins = [2147483647]
amount = 2
Case 8
coins = [1,2147483647]
amount = 2
Case 9
coins = [2147483647,1]
amount = 900
Case 10
coins = [1]
amount = 900
Case 11
coins = [1,2,5]
amount = 900
Case 12
coins = [3,7,405,436]
amount = 2700
Case 13
coins = [411,412,413,414,415,416,417,418,419,420,421,422]
amount = 9864
Case 14
coins = [2]
amount = 1
Case 15
coins = [5,10]
amount = 3
Case 16
coins = [3]
amount = 2000
Case 17
coins = [9999]
amount = 10000
Case 18
coins = [10000]
amount = 10000
Case 19
coins = [12,10000]
amount = 10000
Case 20
coins = [2,5,10,1]
amount = 27
Case 21
coins = [1,5,6,9]
amount = 11
Case 22
coins = [25,10,1]
amount = 30
Case 23
coins = [5,5,1]
amount = 11
Case 24
coins = [12,14,16,18]
amount = 9999
Case 25
coins = [2147483647,2147483646,2147483645]
amount = 10000
Case 26
coins = [7]
amount = 0
Case 27
coins = [4,7]
amount = 5
Case 28
coins = [6,9,20]
amount = 43
Case 29
coins = [13,17,405,436]
amount = 8839
Case 30
coins = [11,23,37]
amount = 9871
Case 31
coins = [2,5]
amount = 1799
Case 32
coins = [461,168,382,62,133,57,36]
amount = 8166
Case 33
coins = [2903,1763,4931,3297,3143,3836,801]
amount = 7287
Case 34
coins = [10,27,40,56,63,31,43,42,68,21,55]
amount = 7738
Case 35
coins = [3975,2498,2177,3815,2659,1208,2678,3359,2721,3252]
amount = 9327
Case 36
coins = [4608,1783,708,731,473,807,3723,4802,3343,4336,35]
amount = 5015
Case 37
coins = [141,395,286,425,204]
amount = 9807
Case 38
coins = [164,257,346,389,237]
amount = 7891
Case 39
coins = [2051,4714,2563,3361,4282,3499,2764,2688]
amount = 5368
Case 40
coins = [53,37,47,18,63,59,57,27,33,17]
amount = 6339
Case 41
coins = [25,27,8]
amount = 8
Case 42
coins = [15]
amount = 47
Case 43
coins = [9,12]
amount = 43
Case 44
coins = [5,21]
amount = 87
Case 45
coins = [20,3,15,14,23]
amount = 53