1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <iostream>
#include <vector>
class Solution {
public:
long long maxPoints(vector<vector<int>>& points) {
int m = points.size();
int n = points[0].size();
std::vector<long long> dp(n);
std::vector<long long> tmp(n);
long long maxe;
for (int i = 0; i < m; ++i) {
std::swap(tmp, dp);
maxe = 0x8000'0000'0000'0000;
for (int j = 0; j < n; ++j) {
maxe = std::max(maxe, tmp[j] + j);
dp[j] = std::max(dp[j], maxe + points[i][j] - j);
}
maxe = 0x8000'0000'0000'0000;
for (int j = n - 1; j >= 0; --j) {
maxe = std::max(maxe, tmp[j] - j);
dp[j] = std::max(dp[j], maxe + points[i][j] + j);
}
}
long long max_score = 0;
for (long long i : dp) {
max_score = std::max(max_score, i);
}
return max_score;
}
};
|