2140. Solving Questions With Brainpower LeetCode Solution
In this guide, you will get 2140. Solving Questions With Brainpower LeetCode Solution with the best time and space complexity. The solution to Solving Questions With Brainpower problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Solving Questions With Brainpower solution in C++
- Solving Questions With Brainpower solution in Java
- Solving Questions With Brainpower solution in Python
- Additional Resources
Problem Statement of Solving Questions With Brainpower
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.
For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:
If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.
If instead, question 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 <= 105
questions[i].length == 2
1 <= pointsi, brainpoweri <= 105
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2140. Solving Questions With Brainpower LeetCode Solution in C++
class Solution {
public:
long long mostPoints(vector<vector<int>>& questions) {
const int n = questions.size();
// dp[i] := the maximum points starting from questions[i]
vector<long> dp(n + 1);
for (int i = n - 1; i >= 0; --i) {
const int points = questions[i][0];
const int brainpower = questions[i][1];
const int nextIndex = i + brainpower + 1;
const long nextPoints = nextIndex < n ? dp[nextIndex] : 0;
dp[i] = max(points + nextPoints, dp[i + 1]);
}
return dp[0];
}
};
/* code provided by PROGIEZ */
2140. Solving Questions With Brainpower LeetCode Solution in Java
class Solution {
public long mostPoints(int[][] questions) {
final int n = questions.length;
// dp[i] := the maximum points starting from questions[i]
long[] dp = new long[n + 1];
for (int i = n - 1; i >= 0; --i) {
final int points = questions[i][0];
final int brainpower = questions[i][1];
final int nextIndex = i + brainpower + 1;
final long nextPoints = nextIndex < n ? dp[nextIndex] : 0;
dp[i] = Math.max(points + nextPoints, dp[i + 1]);
}
return dp[0];
}
}
// code provided by PROGIEZ
2140. Solving Questions With Brainpower LeetCode Solution in Python
class Solution:
def mostPoints(self, questions: list[list[int]]) -> int:
n = len(questions)
# dp[i] := the maximum points starting from questions[i]
dp = [0] * (n + 1)
for i in reversed(range(n)):
points, brainpower = questions[i]
nextIndex = i + brainpower + 1
nextPoints = dp[nextIndex] if nextIndex < n else 0
dp[i] = max(points + nextPoints, dp[i + 1])
return dp[0]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.