2140. Solving Questions With Brainpower - Explanation
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: 10Explanation: 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: 8Explanation: 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: 7Explanation: 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
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
- Define a recursive function starting at index
0. - Base case: if the index exceeds the array length, return
0. - 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.
- Skip: recursively call for
- Return the maximum of the two options.
- The answer is the result of calling the function from index
0.
Time & Space Complexity
- Time complexity:
- Space complexity:
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
- Create a memoization dictionary or array to cache results.
- Define a recursive function that first checks if the result for the current index is already cached.
- If cached, return the stored value.
- Otherwise, compute the maximum of skipping versus solving the current question.
- Store the result in the cache before returning.
- Return the value computed from index
0.
Time & Space Complexity
- Time complexity:
- Space complexity:
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
- Create a
dparray of sizen + 1, initialized to0. - Iterate from the last question to the first (right to left).
- For each question at index
i:- Calculate the points if we solve it:
points[i] + dp[i + 1 + brainpower[i]](or0if out of bounds). - Calculate the points if we skip it:
dp[i + 1]. - Set
dp[i]to the maximum of these two values.
- Calculate the points if we solve it:
- Return
dp[0], which contains the maximum points starting from the first question.
Time & Space Complexity
- Time complexity:
- Space complexity:
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.
Sign in to join the discussion