Coin Change

Given coin denominations and an amount, return the fewest number of coins needed to make that amount. Return -1 if it cannot be formed. This is the standard unbounded knapsack minimization problem.

Input Format

coins = denominations, amount = target amount

Output Format

minimum number of coins needed, or -1

Constraints

  • 1 <= coins.length <= 12; 1 <= coins[i] <= 2^31 - 1; 0 <= amount <= 10^4

Examples

Example 1:

Input:

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

Output:

3

Explanation:

11 = 5 + 5 + 1.

Example 2:

Input:

coins = [2]
amount = 3

Output:

-1

Explanation:

Amount 3 cannot be formed using only 2s.

Loading...
Coin Change - Dp Knapsack DSA Problem