From fedadf69ecb127e8f865305508c3f6c35e14e3fe Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Sun, 13 Feb 2022 23:06:40 -0800 Subject: [PATCH] 2140 --- .../README.md | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/leetcode/2140. Solving Questions With Brainpower/README.md b/leetcode/2140. Solving Questions With Brainpower/README.md index 441a6d2c..5f433339 100644 --- a/leetcode/2140. Solving Questions With Brainpower/README.md +++ b/leetcode/2140. Solving Questions With Brainpower/README.md @@ -67,15 +67,36 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points. class Solution { public: long long mostPoints(vector>& A) { - long long N = A.size(); - vector dp(N + 2); + int N = A.size(); + vector dp(N + 2); for (int i = 0; i < N; ++i) { - int next = i + A[i][1] + 2; - if (next > N) next = N + 1; dp[i + 1] = max(dp[i + 1], dp[i]); + int next = min(i + A[i][1] + 2, N + 1); dp[next] = max(dp[next], dp[i + 1] + A[i][0]); } - return *max_element(begin(dp), end(dp)); + return dp[N + 1]; + } +}; +``` + +Or + +```cpp +// OJ: https://leetcode.com/problems/solving-questions-with-brainpower/ +// Author: github.com/lzl124631x +// Time: O(N) +// Space: O(N) +class Solution { +public: + long long mostPoints(vector>& A) { + int N = A.size(); + vector dp(N + 1); + for (int i = N - 1; i >= 0; --i) { + dp[i] = max(dp[i], dp[i + 1]); + int next = min(i + A[i][1] + 1, N); + dp[i] = max(dp[i], dp[next] + A[i][0]); + } + return dp[0]; } }; ``` \ No newline at end of file