772. Coin Change II — Count Ways (Modulo 10^9+7)

MediumRecursionRecursion

Given an integer array coins of distinct positive denominations and a non-negative integer amount, return the number of DISTINCT combinations (multisets of coins) that sum to amount, taken modulo 10^9 + 7. Each coin may be used an UNLIMITED number of times. Two combinations are considered the same if they contain the same multiset of coin values. By convention, amount = 0 has exactly 1 way (take no coins). Implement with recursion + memoization.

Input: An integer array coins and a non-negative integer amount, formatted as "[c1,c2,...], amount".

Output: Return an integer count mod 10^9 + 7.

Examples

Example 1
Input: [1,2,5], 5
Output: 4
Explanation: 5, 2+2+1, 2+1+1+1, 1+1+1+1+1.
Example 2
Input: [2], 3
Output: 0
Explanation: Cannot make 3.
Example 3
Input: [10], 10
Output: 1
Explanation: Single way: use one coin.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →