Skip to main content

Leetcode 2140

· One min read
Junhe Chen

Question is very easy to solve with dp you only have 2 option and you cache the reapeated branch and it would be solved.

class Solution {
long[] memo;
public long mostPoints(int[][] questions) {
memo = new long[questions.length];
Arrays.fill(memo,-1);
return dp(questions,0);
}
private long dp(int[][] questions, int curr){
// base case
if(curr >= questions.length) return 0; // no more questions
if(memo[curr] != -1) return memo[curr];
// two case
long skip = dp(questions,curr + 1);
long take = questions[curr][0] + dp(questions,curr + questions[curr][1] + 1);
return memo[curr] = Math.max(skip,take);
}
}