题目:
题解:
分为正向dp和倒序dp,具体可看思路:
代码如下:
class Solution {
public:
using LL = long long;
long long mostPoints_1(vector<vector<int>>& que) {
int n=que.size();
LL f[n+1];
memset(f,0,sizeof f);
for(int i=n-1;i>=0;i--){
auto& q=que[i];
int j=i+q[1]+1;
f[i]=max(f[i+1],q[0]+(j<n?f[j]:0));
}
return f[0];
}
long long mostPoints(vector<vector<int>>& que){
int n=que.size();
LL f[n+1];
memset(f,0,sizeof f);
for(int i=0;i<n;++i){
f[i+1]=max(f[i+1],f[i]);
auto &q=que[i];
int j=min(n,i+q[1]+1);
f[j]=max(f[j],f[i]+q[0]);
}
return f[n];
}
};
|