We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents ed2bdc1 + 74cbcd7 commit 985f821Copy full SHA for 985f821
cpp/2140-solving-questions-with-brainpower.cpp
@@ -0,0 +1,28 @@
1
+// Time Complexity: O(n)
2
+// Space Complexity: O(n)
3
+
4
+class Solution
5
+{
6
+public:
7
+ long long mostPoints(vector<vector<int>> &questions)
8
+ {
9
+ int n = questions.size();
10
+ vector<long long> dp(n, 0);
11
+ dp[n - 1] = questions[n - 1][0];
12
+ long long ans = dp[n - 1];
13
+ for (int i = n - 2; i >= 0; i--)
14
15
+ int k = i + questions[i][1] + 1;
16
+ if (k < n)
17
18
+ dp[i] = (dp[k] + questions[i][0]) > dp[i + 1] ? (dp[k] + questions[i][0]) : dp[i + 1];
19
+ }
20
+ else
21
22
+ dp[i] = questions[i][0] > dp[i + 1] ? questions[i][0] : dp[i + 1];
23
24
+ ans = ans > dp[i] ? ans : dp[i];
25
26
+ return ans;
27
28
+};
0 commit comments