You are given a 0-indexed 2D integer array questions where questions[i] = [points_i, brainpower_i]. You go through the questions in order, starting at question 0.
At each question i you make exactly one decision:
-
Skip it and move on to question
i + 1
.
-
Solve it, earn
points_i
points, and then you cannot answer the next
brainpower_i
questions, so you continue at question
i + brainpower_i + 1
. If that index is past the end of the array, the exam is over.
Return the maximum total points you can earn. The total can be larger than what fits in a 32-bit integer, so use a 64-bit result in languages with fixed-width integers.
Function Signature
def most_points(questions: list[list[int]]) -> int:
Constraints
-
1 <= len(questions) <= 10^5
-
len(questions[i]) == 2
-
1 <= points_i, brainpower_i <= 10^5
Examples
Input: questions = [[7,2],[5,1],[9,1],[6,1]]
Output: 13
Solving question 0 (+7) locks out questions 1 and 2, so you continue at question 3 and solve it (+6) for a total of 13. Starting with question 1 instead gives at most 5 + 6 = 11, and solving question 2 gives 9 because it locks out question 3.
Input: questions = [[8,100]]
Output: 8
There is only one question. Solving it earns 8, and the jump to index 101 ends the exam, while skipping it earns 0.
Input: questions = [[1,1],[5,1],[1,1]]
Output: 5
Solving question 0 locks out question 1, so any plan that starts there earns at most 1 + 1 = 2. Skipping question 0 and solving question 1 earns 5.