-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathmaximum-score-from-grid-operations.cpp
70 lines (68 loc) · 3.23 KB
/
maximum-score-from-grid-operations.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Time: O(n^3)
// Space: O(n)
// prefix sum, dp
class Solution {
public:
long long maximumScore(vector<vector<int>>& grid) {
vector<int64_t> prefix(size(grid)+ 1);
for (int i = 0; i < size(grid); ++i) {
prefix[i + 1] = prefix[i] + grid[i][0];
}
// dp[0][i]: the maximum score from 0 to the current column, and the current column has i black cells, without scoring the white cells of the current column
// dp[1][i]: the maximum score from 0 to the current column, and the current column has i black cells, with scoring the white cells of the current column
vector<vector<int64_t>> dp(2, vector<int64_t>(size(grid) + 1));
for (int j = 1; j < size(grid[0]); ++j) {
vector<int64_t> new_prefix(size(grid)+ 1);
for (int i = 0; i < size(grid); ++i) {
new_prefix[i + 1] = new_prefix[i] + grid[i][j];
}
vector<vector<int64_t>> new_dp(2, vector<int64_t>(size(grid) + 1));
for (int i = 0; i <= size(grid); ++i) {
for (int k = 0; k < i + 1; ++k) {
new_dp[0][i] = max(new_dp[0][i], (prefix[i] - prefix[k]) + dp[0][k]);
}
new_dp[0][i] = max(new_dp[0][i], ranges::max(dp[1]));
for (int k = i + 1; k <= size(grid); ++k) {
new_dp[1][i] = max(new_dp[1][i], dp[1][k] + (new_prefix[k] - new_prefix[i]));
}
new_dp[1][i] = max(new_dp[1][i], new_dp[0][i]);
}
dp = move(new_dp);
prefix = move(new_prefix);
}
return ranges::max(dp[1]);
}
};
// Time: O(n^3)
// Space: O(n)
// prefix sum, dp
class Solution2 {
public:
long long maximumScore(vector<vector<int>>& grid) {
vector<int64_t> prefix(size(grid)+ 1);
for (int i = 0; i < size(grid); ++i) {
prefix[i + 1] = prefix[i] + grid[i][0];
}
// dp[0][i]: the maximum score from 0 to the current column, and the current column has i black cells, without scoring the white cells of the current column
// dp[1][i]: the maximum score from 0 to the current column, and the current column has i black cells, with scoring the white cells of the current column
vector<vector<int64_t>> dp(2, vector<int64_t>(size(grid) + 1));
for (int j = 1; j < size(grid[0]); ++j) {
vector<int64_t> new_prefix(size(grid)+ 1);
for (int i = 0; i < size(grid); ++i) {
new_prefix[i + 1] = new_prefix[i] + grid[i][j];
}
vector<vector<int64_t>> new_dp(2, vector<int64_t>(size(grid) + 1));
for (int i = 0; i <= size(grid); ++i) {
for (int k = 0; k <= size(grid); ++k) {
new_dp[0][i] = max(new_dp[0][i], max(prefix[i] - prefix[k], static_cast<int64_t>(0)) + dp[0][k]);
new_dp[1][i] = max(new_dp[1][i], dp[1][k] + max(new_prefix[k] - new_prefix[i], static_cast<int64_t>(0)));
}
new_dp[0][i] = max(new_dp[0][i], ranges::max(dp[1]));
new_dp[1][i] = max(new_dp[1][i], new_dp[0][i]);
}
dp = move(new_dp);
prefix = move(new_prefix);
}
return ranges::max(dp[1]);
}
};