Coin Change II

Given coin denominations and an amount, return the number of combinations that make up the amount. Coins can be used unlimited times, and the order of coins does not matter. This problem is central to understanding why loop order changes the DP meaning.

Input Format

coins = denominations, amount = target

Output Format

number of combinations to form the amount

Constraints

  • 1 <= coins.length <= 300; 1 <= coins[i] <= 5000; 0 <= amount <= 5000

Examples

Example 1:

Input:

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

Output:

4

Explanation:

The combinations are 5, 2+2+1, 2+1+1+1, and 1+1+1+1+1.

Example 2:

Input:

amount = 3
coins = [2]

Output:

0

Explanation:

No combination can form 3 using only 2s.

Loading...
Coin Change II - Dp Knapsack DSA Problem