You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri].
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 pointsi points but you will be unable to solve each of the next brainpoweri questions. If you skip question i, you get to make the decision on the next question.
questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:
0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.Return the maximum points you can earn for the exam.
Example 1:
Input: questions = [[3,2],[4,3],[4,4],[2,5]] Output: 5 Explanation: The maximum points can be earned by solving questions 0 and 3. - Solve question 0: Earn 3 points, will be unable to solve the next 2 questions - Unable to solve questions 1 and 2 - Solve question 3: Earn 2 points Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
Example 2:
Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]] Output: 7 Explanation: The maximum points can be earned by solving questions 1 and 4. - Skip question 0 - Solve question 1: Earn 2 points, will be unable to solve the next 2 questions - Unable to solve questions 2 and 3 - Solve question 4: Earn 5 points Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
Constraints:
1 <= questions.length <= 105questions[i].length == 21 <= pointsi, brainpoweri <= 105This approach leverages dynamic programming to solve the problem. You create a DP array where each element dp[i] represents the maximum points that can be earned starting from question i to the last question. You decide whether to solve or skip a question based on the potential points you can earn. The solution is filled backward, starting from the last question.
The code initializes a dp array with zeros of size n+1 where n is the number of questions. It iterates over the questions in reverse order, calculating the maximum possible points by considering either solving or skipping the current question. Finally, it returns the result stored in dp[0] which represents the maximum points from the first question.
C
C++
Java
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
This approach uses recursion with memoization to avoid recomputing results for overlapping subproblems. It recursively decides the maximum points by considering both solving and skipping options for each question, and stores results in a memoization array for reuse. This provides an efficient way to solve the problem since it avoids redundant calculations.
The Python solution utilizes a recursive function dfs which includes a memoization process. The result of each recursion is cached in the memo dictionary, and recursive calls are made to either solve or skip the current question, while deciding the optimal solution based on maximum points achievable. Finally, the function returns the highest points computed from position zero.
C
C++
Java
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n) |
| Recursion with Memoization Approach | Time Complexity: O(n) |
Solving Questions with Brainpower - Leetcode 2140 - Python • NeetCodeIO • 10,114 views views
Watch 9 more video solutions →Practice Solving Questions With Brainpower with our built-in code editor and test cases.
Practice on FleetCode