Problem Description
Given a list of questions where each question has two properties: points and brainpower, you must process the questions sequentially. When you choose to solve a question, you earn its points but then must skip the next brainpower number of questions. Alternatively, you can skip a question to consider the next one. The goal is to maximize the total points earned.
Key Insights
- Use dynamic programming to decide for each question whether to solve or skip.
- The decision at each question depends on future questions, making it ideal for a backward DP approach.
- Maintain a DP array where dp[i] represents the maximum points from question i to the end.
- For each question, compare the result of solving the current question (points plus dp at the index after the brainpower skip) with skipping it (dp[i+1]).
Space and Time Complexity
Time Complexity: O(n) where n is the number of questions.
Space Complexity: O(n) for the DP array.
Solution
We use a dynamic programming approach that processes the questions in reverse order. For each question at index i:
- Calculate the index you would reach if you decide to solve the question: jumpIndex = i + brainpower[i] + 1.
- The option to solve the question earns you points[i] plus the maximum points from jumpIndex (or 0 if jumpIndex is out of bounds).
- The option to skip the question simply gives you dp[i+1].
- Set dp[i] as the maximum between these two options. The final answer is stored in dp[0].