709. Coin Combinations

Medium3.5 points estimatedArrayDynamic Programming3 hints

A cashier must pay out exactly amount units of money using coins of the denominations listed in coins, with as many coins of each denomination as needed. Two payments are the same when they use each denomination the same number of times, regardless of the order in which the coins are handed over. So 1 + 2 and 2 + 1 are a single payment.

Return the number of different payments that add up to exactly amount, modulo 1000000007. For amount equal to 0 there is one payment, the empty one. Equal values appearing several times in coins are one denomination and must not be counted twice.

Aim for time O(amount * k) for k denominations and extra space O(amount).

Example 1

Input:
amount = 9, coins = [2,3,6]
Output:
3
Explanation:

The payments are 3+3+3, 3+6 and 2+2+2+3, so there are 3.

Example 2

Input:
amount = 4, coins = [5,7]
Output:
0
Explanation:

Every coin is larger than 4, so no payment exists and the answer is 0.

Constraints

1 ≤ coins.length ≤ 50

1 ≤ coins[i] ≤ 1000

0 ≤ amount ≤ 105

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 6,000 msC++ 1,500 msJava 3,000 msJavaScript 3,000 msTypeScript 3,000 ms

Expected complexity

Time
O(amount * k)
Space
O(amount)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…