Number of Ways to Earn Points - Problem
Number of Ways to Earn Points
Imagine you're taking an exam where you need to earn exactly a target number of points. The exam has
• Each question type
• Each question of type
• Questions of the same type are indistinguishable
Your goal is to determine how many different ways you can select questions to earn exactly the target points. Since the answer can be very large, return it modulo
Example: If you have 3 questions of type A (worth 2 points each), selecting questions 1&2 is the same as selecting questions 1&3 or 2&3.
Imagine you're taking an exam where you need to earn exactly a target number of points. The exam has
n different types of questions, each with specific characteristics:• Each question type
i has count[i] available questions• Each question of type
i is worth marks[i] points• Questions of the same type are indistinguishable
Your goal is to determine how many different ways you can select questions to earn exactly the target points. Since the answer can be very large, return it modulo
109 + 7.Example: If you have 3 questions of type A (worth 2 points each), selecting questions 1&2 is the same as selecting questions 1&3 or 2&3.
Input & Output
example_1.py — Basic Case
$
Input:
target = 6, types = [[6,1],[3,2],[2,3]]
›
Output:
7
💡 Note:
You can choose: 6 type1 + 0 type2 + 0 type3 = 6*1 = 6 ✓, 4 type1 + 1 type2 + 0 type3 = 4*1 + 1*2 = 6 ✓, 2 type1 + 2 type2 + 0 type3 = 2*1 + 2*2 = 6 ✓, 0 type1 + 3 type2 + 0 type3 = 0*1 + 3*2 = 6 ✓, 3 type1 + 0 type2 + 1 type3 = 3*1 + 1*3 = 6 ✓, 1 type1 + 1 type2 + 1 type3 = 1*1 + 1*2 + 1*3 = 6 ✓, 0 type1 + 0 type2 + 2 type3 = 0*1 + 0*2 + 2*3 = 6 ✓
example_2.py — Simple Case
$
Input:
target = 5, types = [[50,1],[3,4]]
›
Output:
4
💡 Note:
Ways: (5 type1, 0 type2), (1 type1, 1 type2) = 1+4=5. Only these 2 ways work since we can't use fractional questions.
example_3.py — Edge Case
$
Input:
target = 18, types = [[6,1],[3,2],[2,3]]
›
Output:
1
💡 Note:
Only one way: use all questions = 6*1 + 3*2 + 2*3 = 6+6+6 = 18
Constraints
- 1 ≤ types.length ≤ 50
- 1 ≤ target ≤ 1000
- types[i] = [counti, marksi]
- 1 ≤ counti, marksi ≤ 50
Visualization
Tap to expand
Understanding the Visualization
1
Setup Your Strategy
Initialize with one way to get 0 points (answer nothing)
2
Consider Each Question Type
For each type, explore all possible ways to use 0, 1, 2, ... questions
3
Build Up Scores
Update the number of ways to achieve each possible total score
4
Find Your Answer
The result is the number of ways to achieve exactly target points
Key Takeaway
🎯 Key Insight: Transform the bounded knapsack problem from "find maximum value" to "count ways to reach exact target" using dynamic programming state transitions.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code