Skip to main content

Coin Change II

MediumArrayMath

Description

Given an integer array coins representing different denominations and an integer amount, return the number of combinations that make up that amount.

Examples

Input:amount = 5, coins = [1,2,5]
Output:4
Explanation:

4 ways: 5=5, 5=2+2+1, 5=2+1+1+1, 5=1+1+1+1+1.

Input:amount = 3, coins = [2]
Output:0
Explanation:

The only coin is 2, but 3 is odd and cannot be made with any number of 2s. No valid combination exists.

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

4 ways to make amount 4: 4=3+1, 4=2+2, 4=2+1+1, 4=1+1+1+1. This example demonstrates how multiple coin denominations can create various combinations.

Constraints

  • 1 ≤ coins.length ≤ 300
  • 1 ≤ coins[i] ≤ 5000
  • 0 ≤ amount ≤ 5000

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.