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 pointsi points but skip the next brainpoweri questions
  • 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
Strategic Decision MakingEach question is a strategic choice: immediate gain vs future opportunitiesQuestion TimelineQ03ptsBrainpower: 2Q14ptsBlocked!Q24ptsBlocked!Q32ptsAvailable!Strategy: Solve Q0 (3 pts) + Solve Q3 (2 pts) = 5 points totalAlternative: Skip Q0, Solve Q1 (4 pts), but block Q2&Q3 = 4 points total๐Ÿง  Key Insight: Dynamic ProgrammingWork backwards to know future opportunitiesFor each question: max(solve + future_optimal, skip + next_optimal)
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.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
52.0K Views
Medium Frequency
~18 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