Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/96b65d1f36eb05643ceecc9f0dd7b75f to your computer and use it in GitHub Desktop.
Save SuryaPratapK/96b65d1f36eb05643ceecc9f0dd7b75f to your computer and use it in GitHub Desktop.
class Solution {
#define ll long long
ll findMaxPoints(vector<vector<int>>& questions,int pos,vector<ll>& mem){
if(pos >= questions.size())
return 0;
if(mem[pos]!=-1)
return mem[pos];
ll exclude = findMaxPoints(questions,pos+1,mem);
ll include = questions[pos][0] + findMaxPoints(questions,pos+questions[pos][1]+1,mem);
return mem[pos] = max(exclude,include);
}
public:
long long mostPoints(vector<vector<int>>& questions) {
int n = questions.size();
vector<ll> mem(n,-1);
return findMaxPoints(questions,0,mem);
}
};
/*
//JAVA
import java.util.Arrays;
class Solution {
private long findMaxPoints(int[][] questions, int pos, long[] mem) {
if (pos >= questions.length) {
return 0;
}
if (mem[pos] != -1) {
return mem[pos];
}
long exclude = findMaxPoints(questions, pos + 1, mem);
long include = questions[pos][0] + findMaxPoints(questions, pos + questions[pos][1] + 1, mem);
mem[pos] = Math.max(exclude, include);
return mem[pos];
}
public long mostPoints(int[][] questions) {
int n = questions.length;
long[] mem = new long[n];
Arrays.fill(mem, -1);
return findMaxPoints(questions, 0, mem);
}
}
#Python
class Solution:
def mostPoints(self, questions: List[List[int]]) -> int:
n = len(questions)
mem = [-1] * n
def findMaxPoints(pos):
if pos >= n:
return 0
if mem[pos] != -1:
return mem[pos]
exclude = findMaxPoints(pos + 1)
include = questions[pos][0] + findMaxPoints(pos + questions[pos][1] + 1)
mem[pos] = max(exclude, include)
return mem[pos]
return findMaxPoints(0)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment