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 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
Number of Ways to Earn Points INPUT target = 6 types array Type 0: [6, 1] count=6, marks=1 each ... Type 1: [3, 2] count=3, marks=2 each Type 2: [2, 3] count=2, marks=3 each Goal: Select questions to earn exactly 6 points Answer modulo 10^9 + 7 ALGORITHM (DP) 1 Initialize DP array dp[0]=1, dp[1..6]=0 2 For each question type Process types 0, 1, 2 3 For each count (k) Add k*marks to points 4 Update dp[j] dp[j] += dp[j - k*marks] DP Table (after all types) j: 0 1 2 3 4 5 6 dp: 1 1 2 3 4 5 7 Recurrence: dp[j] += dp[j - k*m] for 0 <= k <= count FINAL RESULT 7 7 different ways to earn exactly 6 points All 7 combinations: 1. 6 of type0 (6x1=6) 2. 3 of type1 (3x2=6) 3. 2 of type2 (2x3=6) 4. 4 type0 + 1 type1 5. 2 type0 + 2 type1 6. 3 type0 + 1 type2 7. 1 type1 + 1 type2 (2+3=5)--Not valid OK - Answer verified! Key Insight: This is a bounded knapsack problem. Use 2D DP where dp[i][j] = ways to get j points using first i types. Optimize to 1D: iterate backwards through points to avoid counting same question twice. Time: O(n * target * max_count), Space: O(target). Each type can contribute 0 to count[i] questions. TutorialsPoint - Number of Ways to Earn Points | Dynamic Programming Approach
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 25
52.4K Views
High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen