Dynamic Programming
Leetcode Weekly Contest 335 Q4
Conversation with ChatGPT

There is a test that has n types of questions. You are given an integer target and a 0-indexed 2D integer array types where types[i] = [counti, marksi] indicates that there are counti questions of the ith type, and each one of them is worth marksi points.

Return the number of ways you can earn exactly target points in the exam. Since the answer may be too large, return it modulo 109 + 7.

Note that questions of the same type are indistinguishable.

For example, if there are 3 questions of the same type, then solving the 1st and 2nd questions is the same as solving the 1st and 3rd questions, or the 2nd and 3rd questions.

Example 1:

Input: target = 6, types = [[6,1],[3,2],[2,3]]
Output: 7
Explanation: You can earn 6 points in one of the seven ways:

• Solve 6 questions of the 0th type: 1 + 1 + 1 + 1 + 1 + 1 = 6
• Solve 4 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 1 + 2 = 6
• Solve 2 questions of the 0th type and 2 questions of the 1st type: 1 + 1 + 2 + 2 = 6
• Solve 3 questions of the 0th type and 1 question of the 2nd type: 1 + 1 + 1 + 3 = 6
• Solve 1 question of the 0th type, 1 question of the 1st type and 1 question of the 2nd type: 1 + 2 + 3 = 6
• Solve 3 questions of the 1st type: 2 + 2 + 2 = 6
• Solve 2 questions of the 2nd type: 3 + 3 = 6
Example 2:

Input: target = 5, types = [[50,1],[50,2],[50,5]]
Output: 4
Explanation: You can earn 5 points in one of the four ways:

• Solve 5 questions of the 0th type: 1 + 1 + 1 + 1 + 1 = 5
• Solve 3 questions of the 0th type and 1 question of the 1st type: 1 + 1 + 1 + 2 = 5
• Solve 1 questions of the 0th type and 2 questions of the 1st type: 1 + 2 + 2 = 5
• Solve 1 question of the 2nd type: 5
Example 3:

Input: target = 18, types = [[6,1],[3,2],[2,3]]
Output: 1
Explanation: You can only earn 18 points by answering all questions.

Constraints:

1 <= target <= 1000
n == types.length
1 <= n <= 50
types[i].length == 2
1 <= count_i, marks_i <= 50

``````class Solution:
def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:
``````

Approach:
We can solve this problem using dynamic programming.

Let dp[i] be the number of ways to get i points.

For each type [count, marks] we can update the dp array as follows:

For each i from marks to target, we can add count ways to get i - marks points using this type.

So dp[i] += dp[i - marks] * count.

The final answer is dp[target] % (10^9 + 7).

Time Complexity: O(target * n), where n is the number of types.

Space Complexity: O(target)

Let's see the implementation.

Implement it

Here's the Python implementation of the above approach:

``````from typing import List

class Solution:
def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:
dp =  * (target + 1)
dp = 1

for count, marks in types:
for i in range(marks, target + 1):
dp[i] += dp[i - marks] * count
dp[i] %= 1000000007

return dp[target]
``````

6
[[6,1],[3,2],[2,3]]
51367
7

I apologize for the mistake in my previous response. Here is the corrected implementation:

``````class Solution:
def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:
dp =  * (target + 1)
dp = 1

for count, marks in types:
for i in range(target, marks - 1, -1): # iterating backwards to avoid using the same type multiple times
for j in range(1, count + 1):
if i - j * marks < 0:
break
dp[i] += dp[i - j * marks]
dp[i] %= 10**9 + 7

return dp[target]
``````

I have updated the inner loop to iterate backwards and added a third loop to iterate over the number of questions of each type used. This ensures that we don't use the same type multiple times and avoids overcounting. The time complexity of this implementation is O(target * n * k), where k is the maximum number of questions of a single type used.

Submission Result: Accepted

Great to hear that! I'm glad that my updated implementation worked and the submission was successful. If you have any more questions or need further assistance, feel free to ask! 