# 2140. Solving Questions With Brainpower ###### tags: `Leetcode` `Medium` `Dynamic Programming` Link: https://leetcode.com/problems/solving-questions-with-brainpower/ ## 思路 $O(N)$ $O(N)$ For each index $i$ we have 2 options: - Take $points_i$ and jump the next $brainpower_i$ indexes - Skip the current index(do not collect $points_i$) and move to the next index We need to find the maximum points we can collect given the above mentioned constraints Note: $dp[i]$ denotes the max points that can be collected for the subarray: $questions_{[i... questions.size() - 1]}$. So we only need to compute it once for each $i$ ## Code ```java= class Solution { public long mostPoints(int[][] questions) { int n = questions.length; long[] dp = new long[questions.length+1]; for(int i = questions.length-1;i >= 0;i--){ dp[i] = Math.max(questions[i][0]+dp[Math.min(i+questions[i][1]+1,n)], dp[i+1]); } return dp[0]; } } ```