Solving Questions With Brainpower (LeetCode 2140): Solution and Explanation
Each question has a point value and a brainpower cost. Solving question i earns its points but blocks the next brainpower_i questions, while skipping it moves on to question i + 1. Find the largest total you can earn going through the exam in order.
Intuition
A greedy rule such as always solving the biggest question fails, because a big question can block several medium ones whose total is larger. Every decision changes what is available later, so we need to compare whole futures.
The trick is to look from the end. Suppose you already know the best score you can get starting from every question after i. Then the decision at i is easy: solving gives points_i plus the best score starting right after the blocked range, and skipping gives the best score starting at i + 1.
That is a one-dimensional DP filled from right to left. dp[n] = 0 because nothing is left after the last question, and every dp[i] only reads entries to its right, which are already filled.
The sums can reach about 10^5 questions times 10^5 points, well above 2^31, so the table needs 64-bit integers in Java, C++, C# and Go.
Approach: Right-to-left dynamic programming
- Create
dpof size n + 1 filled with 0.dp[i]is the best score using questions i through n - 1. - For
ifrom n - 1 down to 0, computenext = i + brainpower_i + 1. solve = points_i + dp[next]whennext < n, otherwise justpoints_i.skip = dp[i + 1]. Setdp[i] = max(solve, skip).- Return
dp[0].
Step-by-step example
questions = [[3,2],[4,3],[4,4],[2,5]]
- dp[4] = 0 because there are no questions after the last one.
- i = 3: solve = 2 + 0 = 2 (next = 9 is past the end), skip = dp[4] = 0, so dp[3] = 2.
- i = 2: solve = 4 + 0 = 4 (next = 7), skip = dp[3] = 2, so dp[2] = 4.
- i = 1: solve = 4 + 0 = 4 (next = 5), skip = dp[2] = 4, so dp[1] = 4.
- i = 0: solve = 3 + dp[3] = 5 (next = 3), skip = dp[1] = 4, so dp[0] = 5.
- Replaying the choices: solve question 0 (blocks 1 and 2), then solve question 3. The answer is 5.
Code: Python, Java, C++
1class Solution:
2 def mostPoints(self, questions: List[List[int]]) -> int:
3 n = len(questions)
4 dp = [0] * (n + 1)
5 for i in range(n - 1, -1, -1):
6 points, power = questions[i]
7 nxt = i + power + 1
8 solve = points + (dp[nxt] if nxt < n else 0)
9 dp[i] = max(solve, dp[i + 1])
10 return dp[0]1class Solution {
2 public long mostPoints(int[][] questions) {
3 int n = questions.length;
4 long[] dp = new long[n + 1];
5 for (int i = n - 1; i >= 0; i--) {
6 int next = i + questions[i][1] + 1;
7 long solve = questions[i][0] + (next < n ? dp[next] : 0);
8 dp[i] = Math.max(solve, dp[i + 1]);
9 }
10 return dp[0];
11 }
12}1class Solution {
2public:
3 long long mostPoints(vector<vector<int>>& questions) {
4 int n = questions.size();
5 vector<long long> dp(n + 1, 0);
6 for (int i = n - 1; i >= 0; i--) {
7 int next = i + questions[i][1] + 1;
8 long long solve = questions[i][0] + (next < n ? dp[next] : 0LL);
9 dp[i] = max(solve, dp[i + 1]);
10 }
11 return dp[0];
12 }
13};Time and space complexity
O(n)
Each question is visited once and does constant work: one lookup for solving and one for skipping.
O(n)
The dp array has n + 1 entries. It cannot shrink to O(1) because the solve option can jump arbitrarily far ahead.
Other approaches
Top-down memoisation (time O(n), space O(n))
Recurse with best(i) = max(points_i + best(i + brainpower_i + 1), best(i + 1)) and cache results. Same complexity, but deep recursion on 10^5 questions can overflow the stack in Python without raising the limit.
Forward push DP (time O(n), space O(n))
Keep best[i] as the best score on arrival at i. From each i, push to i + 1 by skipping and to i + brainpower_i + 1 by solving, keeping a running maximum. It works but needs care with indices past the end.
Common mistakes
- Using 32-bit integers. The total can be several billion.
- Forgetting the +1 in
i + brainpower_i + 1. Solving question i blocks brainpower_i questions after it, and the next allowed one is one further. - Indexing
dp[next]without checking that next is within the array, which reads past the end. - Trying a greedy choice from the front, such as taking the question with the best points per blocked question.
Frequently asked questions
Why fill the DP from right to left?
Both options at question i depend on scores of later questions. Going right to left means those scores are already known when you reach i.
How is this different from House Robber?
House Robber always blocks exactly one neighbour. Here each question blocks its own number of followers, so the solve option jumps a different distance at every index, but the solve or skip recurrence has the same shape.
Does the answer need a 64-bit integer?
Yes in typed languages. With up to 10^5 questions worth up to 10^5 points each, the total can exceed 2^31 - 1, so Java and C# return long, C++ returns long long and Go returns int64.
What is the time complexity?
O(n) time and O(n) space, one pass over the questions from the back.