Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
33 lines (27 loc) · 867 Bytes

File metadata and controls

33 lines (27 loc) · 867 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Approch:
2nd robot can collect either all the bottom points
before the break point (when 1st robot goes to bottom)
or collect all the top points after that break point.
Time Complexity: O(N)
Space Complexity: O(1)
*/
class Solution {
public:
long long gridGame(vector<vector<int>>& grid) {
// prefix sum
long long top = grid[0][0],bottom = 0, answer = LONG_MAX;
for(int i =1;i<grid[0].size();i++){
top += grid[0][i];
}
for(int i =0;i<grid[0].size();i++){
// All the top points 2nd robot can collect
top -= grid[0][i];
// min because first robot wants to minimize
answer = min(answer,max(top,bottom));
// All the bootom points the robot could collect
bottom += grid[1][i];
}
return answer;
}
};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.