#518

Coin Change II

specialist · 700 · lc medium +30 · verified · 60.4% accepted · 10,187 likes · top 59%

play →

Description

You are given an integer array coins of available denominations and an integer amount as a monetary target. With an unlimited supply of each coin type, return the total number of distinct combinations that sum exactly to amount. Return 0 if no valid combination exists. The result is guaranteed to fit within a signed 32-bit integer.

Example 1:

Input: amount = 5, coins = [1,2,5]
Output: 4
Explanation: there are four ways to make up the amount:
5=5
5=2+2+1
5=2+1+1+1
5=1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]
Output: 0
Explanation: the amount of 3 cannot be made up just with coins of 2.

Example 3:

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

Code

1
2
3