2140. Solving Questions With Brainpower - Explanation

Problem Link

Description

Process questions from left to right. Each entry contains [points, brainpower]. You may skip a question and consider the next one, or solve it to earn its points. Solving index i forces you to skip the following brainpower questions, so the next index you may consider is i + brainpower + 1. If that index is beyond the array, the exam ends. Return the maximum total points you can earn.

Example 1:

Input: questions = [[5,1],[9,1],[5,1]]

Output: 10

Explanation: Solve questions 0 and 2. Solving question 0 skips question 1, then question 2 adds another 5 points. The total 10 is better than solving question 1 for 9 points.

Example 2:

Input: questions = [[2,3],[8,1],[1,1]]

Output: 8

Explanation: Skip question 0 and solve question 1 for 8 points. Its required skip passes question 2, ending the exam.

Example 3:

Input: questions = [[7,100]]

Output: 7

Explanation: Solve the only question for 7 points. A skip extending beyond the end does not remove points already earned.

Constraints:

  • 1 <= questions.length <= 100000
  • Every entry contains exactly two integers: [points, brainpower].
  • 1 <= points, brainpower <= 100000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Recursion - Breaking problems into smaller subproblems with base cases
  • Dynamic Programming - Optimizing recursive solutions by storing computed results
  • Memoization - Caching function results to avoid redundant calculations
  • Bottom-Up DP - Iteratively building solutions from smaller subproblems to larger ones

1. Recursion

Intuition

At each question, we have two choices: solve it and skip the next few questions based on its brainpower requirement, or skip it entirely and move to the next question. This creates a decision tree where we want to maximize points. We recursively explore both choices at each position and return the maximum.

Algorithm

  1. Define a recursive function starting at index 0.
  2. Base case: if the index exceeds the array length, return 0.
  3. At each index, compute two options:
    • Skip: recursively call for index + 1.
    • Solve: add the current question's points and recursively call for index + 1 + brainpower.
  4. Return the maximum of the two options.
  5. The answer is the result of calling the function from index 0.
class Solution:
    def mostPoints(self, questions: List[List[int]]) -> int:
        def dfs(i):
            if i >= len(questions):
                return 0
            return max(dfs(i + 1), questions[i][0] + dfs(i + 1 + questions[i][1]))
        return dfs(0)

Time & Space Complexity

  • Time complexity: O(2n)O(2 ^ n)
  • Space complexity: O(n)O(n)

2. Dynamic Programming (Top-Down)

Intuition

The plain recursion has overlapping subproblems since we may compute the maximum points from the same index multiple times. By storing results in a memoization table, we avoid redundant calculations. Each index is computed at most once, giving us linear time complexity.

Algorithm

  1. Create a memoization dictionary or array to cache results.
  2. Define a recursive function that first checks if the result for the current index is already cached.
  3. If cached, return the stored value.
  4. Otherwise, compute the maximum of skipping versus solving the current question.
  5. Store the result in the cache before returning.
  6. Return the value computed from index 0.
class Solution:
    def mostPoints(self, questions: List[List[int]]) -> int:
        dp = {}
        def dfs(i):
            if i >= len(questions):
                return 0
            if i in dp:
                return dp[i]
            dp[i] = max(dfs(i + 1), questions[i][0] + dfs(i + 1 + questions[i][1]))
            return dp[i]
        return dfs(0)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

3. Dynamic Programming (Bottom-Up)

Intuition

Instead of recursion, we can fill a DP table iteratively from right to left. For each question, we compute the maximum points achievable starting from that position. Working backwards ensures that when we process question i, we already know the best outcomes for all questions after it.

Algorithm

  1. Create a dp array of size n + 1, initialized to 0.
  2. Iterate from the last question to the first (right to left).
  3. For each question at index i:
    • Calculate the points if we solve it: points[i] + dp[i + 1 + brainpower[i]] (or 0 if out of bounds).
    • Calculate the points if we skip it: dp[i + 1].
    • Set dp[i] to the maximum of these two values.
  4. Return dp[0], which contains the maximum points starting from the first question.
class Solution:
    def mostPoints(self, questions: List[List[int]]) -> int:
        dp = {}

        for i in range(len(questions) - 1, -1, -1):
            dp[i] = max(
                questions[i][0] + dp.get(i + 1 + questions[i][1], 0),
                dp.get(i + 1, 0)
            )
        return dp.get(0)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Common Pitfalls

Integer Overflow with Point Accumulation

The points can accumulate to values exceeding 32-bit integer limits. When summing points across many questions, the result may overflow int. Always use long or long long for the DP array and return type to handle large cumulative values safely.

Incorrect Jump Calculation After Solving a Question

After solving question i, you must skip to question i + 1 + brainpower[i], not i + brainpower[i]. The +1 accounts for moving past the current question before skipping the required number. Missing this causes you to land on a question you should have skipped.

Wrong Iteration Direction in Bottom-Up DP

The bottom-up approach must iterate from the last question to the first (right to left). Iterating left to right means dp[i+1] has not been computed yet when you need it. Always use a reverse loop: for i in range(n-1, -1, -1) in Python or for (int i = n-1; i >= 0; i--) in other languages.