# Leetcode 简单线性递推：正反两种方法

## LC 5982. Solving Questions With Brainpower

276场周赛第3题，差点翻车...

### 方法一：倒序DP（查表）

class Solution {
public:
long long mostPoints(vector<vector<int> >& questions) {
long long ans = 0;
int n = questions.size();
vector<long long>dp(n+1, 0);
dp[n-1] = questions[n-1][0];
for(int i = n-2;i >= 0;i--) {
if(i+questions[i][1] +1 < n)   dp[i] = max(dp[i+1], dp[i+questions[i][1]+1] + questions[i][0]);
else  dp[i] = max(dp[i+1], (long long)questions[i][0]);
}
return dp[0];
}
};


### 方法二：正向刷表

class Solution {
public:
long long mostPoints(vector<vector<int>>& questions) {
int n = questions.size();
vector<long long> dp(n, 0);
for (int i = 0; i < n; i++) {
int next = i + 1;
if (next < n)  dp[next] = max(dp[next], dp[i]);

next = i + questions[i][1] + 1;
dp[i] += questions[i][0];  //
if (next < n)  dp[next] = max(dp[next], dp[i]);
}
long long ans = *max_element(dp.begin(), dp.end()); // 取dp中的最大值
return ans;
}
};


## LC 1751. 最多可以参加的会议数目 II

class Solution {
public:
int maxValue(vector<vector<int>>& events, int k) {
int n = events.size();
sort(events.begin(), events.end(), [](const vector<int>& a, const vector<int>& b) {
if(a[1] == b[1])  return a[0] < b[0];
return a[1] < b[1];
}); // 按结束时间从小到大排序

vector<int>endtimes;
for(int i = 0;i < n;i++)  endtimes.push_back(events[i][1]);
vector<vector<int>>dp(n+1, vector<int>(k+1, 0));
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= k;j++) {
dp[i][j] = dp[i-1][j];   // 不开
int pre = lower_bound(endtimes.begin(), endtimes.end(), events[i-1][0]) - endtimes.begin() - 1;
// cout << "pre: " << pre << endl;
dp[i][j] = max(dp[i][j], dp[pre+1][j-1] + events[i-1][2]);
// cout << i << " " << j << " " << dp[i][j] << endl;
}
}
int ans = -1;
for(int i = 1;i <= k;i++)  ans = max(ans, dp[n][i]);
return ans;
}
};


## LC 1235. 规划兼职工作

class Solution {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
int n = startTime.size();
vector<vector<int>>jobs;
for(int i = 0;i < n;i++) {
jobs.push_back({startTime[i], endTime[i], profit[i]});
}
sort(jobs.begin(), jobs.end(), [](const vector<int>& a, const vector<int>& b){
return a[1] < b[1];
});
vector<int>myendtimes;
for(int i = 0;i < n;i++) myendtimes.push_back(jobs[i][1]);
vector<int>dp(n+1, 0);
for(int i = 1;i <= n;i++) {
dp[i] = dp[i-1];  // 不选
int pre = upper_bound(myendtimes.begin(), myendtimes.end(), jobs[i-1][0]) - myendtimes.begin() - 1;
dp[i] = max(dp[i], dp[pre+1] + jobs[i-1][2]);
}
return dp[n];
}
};

posted @ 2022-01-17 00:41  Rogn  阅读(34)  评论(0编辑  收藏  举报