You are given a 0-indexed 2D integer array questions where questions[i] = [pointsᵢ, brainpowerᵢ].
The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whether to solve or skip each question. Solving question i will earn you pointsᵢ points but you will be unable to solve each of the next brainpowerᵢ questions. If you skip question i, you get to make the decision on the next question.
- For example, given
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:- If question
0is solved, you will earn3points but you will be unable to solve questions1and2. - If instead, question
0is skipped and question1is solved, you will earn4points but you will be unable to solve questions2and3.
- If question
Return the maximum points you can earn for the exam.
Constraints:
1 <= questions.length <= 10⁵questions[i].length == 21 <= pointsᵢ, brainpowerᵢ <= 10⁵
Approach: Dynamic Programming (Bottom-Up)
func mostPoints(questions [][]int) int64 {
n := len(questions)
dp := make([]int64, n+1)
for i := n - 1; i >= 0; i-- {
next := min(i + questions[i][1] + 1, n)
take := int64(questions[i][0]) + dp[next]
dp[i] = max(dp[i+1], take)
}
return dp[0]
}Intuition
The key idea is to use dynamic programming by iterating backwards through the list of questions. For each question, you decide whether to:
- Solve it: Earn the current points and skip the next
brainpowerquestions. - Skip it: Consider the maximum points starting from the next question.
This way, each state dp[i] holds the maximum points you can earn starting at question i.
Algorithm
- Initialization:
Create a DP arraydpof sizen+1(wherenis the number of questions). Initializedp[n]to0since there are no questions after the last one. - Reverse Iteration:
Loop backwards from the last question to the first. - Decision Making:
For each question at indexi:- Solve: Calculate
take = points[i] + dp[i + brainpower[i] + 1].
(Useminto ensure you don't go out of bounds.) - Skip: Use
dp[i+1]which represents not solving the current question. - Update: Set
dp[i]to the maximum of these two choices.
- Solve: Calculate
- Result:
After processing all questions,dp[0]contains the maximum points achievable starting from the first question.
Complexity Analysis
-
Time Complexity:
O(n)– We iterate over each question once in reverse order. -
Space Complexity:
O(n)– We use a DP array of sizen+1to store the computed results.
Wrap up
If you found this guide helpful, consider subscribing to my newsletter on jyotirmoy.dev/blogs , You can also follow me on Twitter jyotirmoydotdev for updates and more content.

