-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3225.cpp
More file actions
45 lines (45 loc) · 1.62 KB
/
3225.cpp
File metadata and controls
45 lines (45 loc) · 1.62 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
long long maximumScore(vector<vector<int>>& grid) {
int n = grid.size();
vector<long long> prevColW(n + 1, 0);
vector<long long> prevColWO(n + 1, 0);
vector<long long> currColW(n + 1, 0);
vector<long long> currColWO(n + 1, 0);
if (n == 1) return 0;
for (int j = 1; j < n; ++j) {
for (int i = 0; i <= n; ++i) {
currColW[i] = 0;
currColWO[i] = 0;
}
for (int i = 0; i <= n; ++i) {
long long prevColVal = 0;
long long currColVal = 0;
for (int p = 0; p < i; ++p) {
currColVal += grid[p][j];
}
for (int k = 0; k <= n; ++k) {
if (k > 0 && k <= i) {
currColVal -= grid[k - 1][j];
}
if (k > i) {
prevColVal += grid[k - 1][j - 1];
}
currColWO[k] = max(currColWO[k], prevColVal + prevColWO[i]);
currColWO[k] = max(currColWO[k], prevColW[i]);
currColW[k] = max(currColW[k], currColVal + prevColW[i]);
currColW[k] = max(currColW[k], currColVal + prevColVal + prevColWO[i]);
}
}
for (int i = 0; i <= n; ++i) {
prevColW[i] = currColW[i];
prevColWO[i] = currColWO[i];
}
}
long long res = 0;
for (int i = 0; i <= n; ++i) {
res = max(res, currColW[i]);
}
return res;
}
};