2140. Solving Questions With Brainpower - Explanation

Problem Link



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.