Skip to content
Decoder

Coin Change II

Medium

Problem

You have unlimited coins of each value in coins. Return the number of combinations that add up to exactly amount. Order does not matter: 1 + 2 and 2 + 1 are the same combination. If no combination works, return 0.

Example 1

Input
amount = 5, coins = [1, 2, 5]
Output
4
Why
5, 2+2+1, 2+1+1+1, 1+1+1+1+1

Example 2

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

Example 3

Input
amount = 10, coins = [10]
Output
1

Constraints

  • 1 <= coins.length <= 300
  • 1 <= coins[i] <= 5000
  • All values in coins are unique.
  • 0 <= amount <= 5000
  • The answer fits in a 32-bit signed integer.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.