-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2017.cpp
More file actions
35 lines (35 loc) · 951 Bytes
/
2017.cpp
File metadata and controls
35 lines (35 loc) · 951 Bytes
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
class Solution {
public:
long long gridGame(vector<vector<int>>& grid) {
int n = grid[1].size();
vector<long long> prefix1(n, 0);
vector<long long> prefix2(n, 0);
prefix2[0] = grid[1][0];
for (int i = 1; i < n; ++i) {
prefix2[i] = prefix2[i - 1] + grid[1][i];
}
prefix1[n - 1] = grid[0][n - 1];
for (int i = n - 2; i >= 0; --i) {
prefix1[i] = prefix1[i + 1] + grid[0][i];
}
long long res = LLONG_MAX;
for (int i = 0; i < n; ++i) {
long long a = 0;
long long b = 0;
if (i == 0) {
b = 0;
}
else {
b = prefix2[i - 1];
}
if (i == n - 1) {
a = 0;
}
else {
a = prefix1[i + 1];
}
res = min(res, max(a, b));
}
return res;
}
};