Coin Change II

You are given an amount and an array coins of distinct coin values, with an unlimited supply of each value.

Return the number of different combinations of coins that add up to exactly amount. A combination only says how many coins of each value are used, so 1 + 2 and 2 + 1 are the same combination. If the amount can't be made, return 0. There is exactly one way to make an amount of 0: use no coins.

Constraints

  • 0 <= amount <= 5000
  • 1 <= coins.length <= 50
  • 1 <= coins[i] <= 5000, all different
  • The answer fits in a 32-bit signed integer.

Examples

Example 1

Input: amount = 6, coins = [1,2,3]
Output: 7

3+3, 3+2+1, 3+1+1+1, 2+2+2, 2+2+1+1, 2+1+1+1+1 and 1+1+1+1+1+1. The order of the coins doesn't matter.

Example 2

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

Coins of 2 only make even amounts.

Example 3

Input: amount = 0, coins = [7]
Output: 1

There is exactly one way to make 0: use no coins.

JavaScript you'll use: Arrays: create, read, update Recursion, the call stack and memoization

/**
 * @param {number} amount
 * @param {number[]} coins
 * @return {number}
 */
function change(amount, coins) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.