Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created April 1, 2025 22:43
Show Gist options
  • Save Ifihan/f738e67128bb224d12fa2a816e75a8cc to your computer and use it in GitHub Desktop.
Save Ifihan/f738e67128bb224d12fa2a816e75a8cc to your computer and use it in GitHub Desktop.
Solving Questions With Brainpower

Question

Approach

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.

Implementation

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]

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment