Coin Change II (Count Ways)

MediumDynamic Programmingunbounded knapsack

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer amount representing a target sum of money, and an integer array coins listing the distinct denominations of coins available. You have an **unlimited** supply of every coin denomination. Return the number of distinct combinations of coins that add up exactly to amount. If no combination can reach the target, return 0. Two combinations are considered the **same** if they use the same multiset of coins, regardless of the order in which the coins are picked. For example, using [1, 2, 2] is the same combination as [2, 1, 2], so it should only be counted once. It is guaranteed that the answer fits within a signed 32-bit integer.

Examples

Input: amount = 5, coins = [1, 2, 5]
Output: 4
The combinations are: 5; 2+2+1; 2+1+1+1; 1+1+1+1+1.
Input: amount = 3, coins = [2]
Output: 0
Only the denomination 2 is available, and no number of 2-coins sums to an odd value like 3.
Input: amount = 0, coins = [7]
Output: 1
There is exactly one way to make 0: pick no coins at all (the empty combination).

Constraints

  • 1 <= coins.length <= 300
  • 1 <= coins[i] <= 5000
  • All values in `coins` are distinct.
  • 0 <= amount <= 5000
  • The answer is guaranteed to fit in a signed 32-bit integer.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.