Solving Questions With Brainpower - Problem
You're taking an exam with n questions arranged in a sequence. Each question has two properties:
- Points: The score you earn for solving it
- Brainpower: How many subsequent questions you must skip if you solve this one
Given a 2D array questions where questions[i] = [pointsi, brainpoweri], you must process questions in order from index 0. For each question, you can either:
- Solve it: Earn
pointsipoints but skip the nextbrainpoweriquestions - Skip it: Move to the next question without earning points
Example: Given [[3,2], [4,3], [4,4], [2,5]]
- If you solve question 0: earn 3 points, skip questions 1 and 2, then decide on question 3
- If you skip question 0 and solve question 1: earn 4 points, skip questions 2 and 3
Return the maximum points you can earn from the entire exam.
Input & Output
example_1.py โ Basic Case
$
Input:
questions = [[3,2],[4,3],[4,4],[2,5]]
โบ
Output:
5
๐ก Note:
Maximum points can be earned by solving questions 1 and 3: skip question 0, solve question 1 (earn 4 points, skip questions 2 and 3), then solve question 3 (earn 2 points). Wait, that's wrong. Actually: solve question 0 (3 points, skip 1,2) + solve question 3 (2 points) = 5. Or solve question 1 (4 points, skip 2,3) = 4. Or solve question 2 (4 points, skip everything after) = 4. The maximum is actually 7: solve question 0 (3 points, skip to question 3) + solve question 3 (2 points) = 5. Wait, let me recalculate: solve question 1 (4 points, skip 2,3) = 4, OR skip 0,1 and solve question 2 (4 points) = 4, OR solve question 0 (3 points) + solve question 3 (2 points) = 5.
example_2.py โ Single Question
$
Input:
questions = [[1,1]]
โบ
Output:
1
๐ก Note:
There's only one question. We can solve it to earn 1 point, or skip it to earn 0 points. The maximum is 1.
example_3.py โ High Brainpower
$
Input:
questions = [[21,5],[92,3],[74,2],[39,4],[58,2],[5,5],[49,4],[65,3]]
โบ
Output:
157
๐ก Note:
With high brainpower values, we need to be strategic about which questions to solve to maximize points while accounting for the skip penalties.
Constraints
- 1 โค questions.length โค 105
- questions[i].length == 2
- 1 โค pointsi, brainpoweri โค 105
- Process questions in order from index 0
Visualization
Tap to expand
Understanding the Visualization
1
Assess Each Question
Look at points gained vs future questions blocked
2
Work Backwards
Start from the end to know future opportunities
3
Make Optimal Choice
Choose solve vs skip based on maximum total benefit
4
Build Complete Strategy
Combine optimal choices to get maximum points
Key Takeaway
๐ฏ Key Insight: Dynamic Programming eliminates the need to recalculate the same subproblems by working backwards and storing optimal results for each position.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code