Skip to content

Commit 1275fca

Browse files
2140_Solving_Questions_with_BrainPower.java
1 parent a39ff21 commit 1275fca

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Problem Number: 2140
2+
3+
// Solving Questions with Brain Power.
4+
5+
class Solution {
6+
public long mostPoints(int[][] questions) {
7+
final int n = questions.length;
8+
// dp[i] := the maximum points starting from questions[i]
9+
long[] dp = new long[n + 1];
10+
11+
for (int i = n - 1; i >= 0; --i) {
12+
final int points = questions[i][0];
13+
final int brainpower = questions[i][1];
14+
final int nextIndex = i + brainpower + 1;
15+
final long nextPoints = nextIndex < n ? dp[nextIndex] : 0;
16+
dp[i] = Math.max(points + nextPoints, dp[i + 1]);
17+
}
18+
19+
return dp[0];
20+
}
21+
}

0 commit comments

Comments
 (0)