2-D Dynamic Programming Knapsack

549. Coin Change II

Medium 147 DSA Sheet

You are given an integer amount and an array coins of distinct coin values. You have as many coins of each value as you want.

Return the number of different mixes of coins that add up to exactly amount. A mix is how many coins of each value you use, so the order of the coins does not matter: 2 + 3 and 3 + 2 are the same mix. An amount of 0 has exactly one mix, the one with no coins.

The number can be very large, so return it modulo 109 + 7.

Example 1

Input: amount = 10, coins = [2, 3, 5]

Output: 4

Explanation: The mixes are 5 + 5, 5 + 3 + 2, 3 + 3 + 2 + 2 and 2 + 2 + 2 + 2 + 2.

Example 2

Input: amount = 12, coins = [5, 10]

Output: 0

Explanation: Every mix of 5s and 10s adds up to a multiple of 5, and 12 is not one.

Example 3

Input: amount = 0, coins = [7]

Output: 1

Explanation: The only mix is the one with no coins.

Check your understanding

Work out the answer for this input before you write any code.

amount = 4, coins = [1, 2]

Constraints

  • 0 ≤ amount ≤ 104
  • 1 ≤ coins.length ≤ 100
  • 1 ≤ coins[i] ≤ 109
  • All the values in coins are distinct.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.