Talentd
FleetCode

3183. The Number of Ways to Make the Sum

Medium50.8% acceptancePremium elsewhere, free here

You have an infinite number of coins with values 1, 2, and 6, and only 2 coins with value 4.

Given an integer n, return the number of ways to make the sum of n with the coins you have.

Since the answer may be very large, return it modulo 109 + 7.

Note that the order of the coins doesn't matter and [2, 2, 3] is the same as [2, 3, 2].

Example 1:

Input: n = 4

Output: 4

Explanation:

Here are the four combinations: [1, 1, 1, 1], [1, 1, 2], [2, 2], [4].

Example 2:

Input: n = 12

Output: 22

Explanation:

Note that [4, 4, 4] is not a valid combination since we cannot use 4 three times.

Example 3:

Input: n = 5

Output: 4

Explanation:

Here are the four combinations: [1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 2, 2], [1, 4].

Constraints:

  • 1 <= n <= 105

Hints open one at a time

Hint 1
Consider using dynamic programming.
Hint 2
Define dp[i][x] as the number of ways to make the sum x using only the first i coins; and define coin[i] as the value of coin i.
Hint 3
We can calculate dp[i][x] as the sum of dp[i - 1][x] and dp[i][x - coin[i]].
Hint 4
Remember that 4 can at most be multiplied twice, so we calculate the dp for our infinite coins and then manually handle the existence of 4.
14:47
Still stuck?Read 2 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
4
Input
12
Input
5
Your code is saved on this device.Solution