To solve this problem, I used dynamic programming, working from the end of the list backwards. For each question, I had two choices:
- Skip it and take the max points from the next question.
- Solve it, earn points[i], and skip the next brainpower[i] questions.
I stored the best decision at each index and returned the result from index 0.
class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
dp = [0] * (n + 1)
for i in range(n - 1, -1, -1):
points, skip = questions[i]
next_q = i + skip + 1
solve = points + (dp[next_q] if next_q < n else 0)
skip = dp[i + 1]
dp[i] = max(solve, skip)
return dp[0]
- Time: O(n)
- Space: O(n)
