Skip to content

Commit a38a947

Browse files
authored
Create grid-game.cpp
1 parent 5e3d82c commit a38a947

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

C++/grid-game.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
long long gridGame(vector<vector<int>>& grid) {
7+
int64_t result = numeric_limits<int64_t>::max();
8+
int64_t left = 0, right = accumulate(cbegin(grid[0]), cend(grid[0]), 0ll);
9+
for (int i = 0; i < size(grid[0]); ++i) {
10+
right -= grid[0][i];
11+
result = min(result, max(left, right));
12+
left += grid[1][i];
13+
}
14+
return result;
15+
}
16+
};

0 commit comments

Comments
 (0)