Skip to content
BASA DSA

Coin Change

Medium1-D Dynamic Programming

Using unlimited copies of each positive coin denomination, return the fewest coins summing exactly to amount. Return -1 if impossible, and zero for amount zero.

Example 1

Input
coins = [2,3,7]
amount = 11
Output
3

Explanation

7+2+2 uses three coins; no one- or two-coin combination reaches 11.

Example 2

Input
coins = [4]
amount = 6
Output
-1

Explanation

Coins worth 4 only produce multiples of 4, never 6.

Example 3

Input
coins = [5]
amount = 0
Output
0

Explanation

Amount zero requires no coins.

Constraints

  • 1 <= number of distinct coins <= 12
  • 0 <= amount <= 10000
  • Coin values are positive signed 32-bit integers

Function contractcoinChangeArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…