File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ Approch:
3+ 2nd robot can collect either all the bottom points
4+ before the break point (when 1st robot goes to bottom)
5+ or collect all the top points after that break point.
6+ Time Complexity: O(N)
7+ Space Complexity: O(1)
8+ */
9+
10+ class Solution {
11+ public:
12+ long long gridGame (vector<vector<int >>& grid) {
13+
14+ // prefix sum
15+ long long top = grid[0 ][0 ],bottom = 0 , answer = LONG_MAX;
16+ for (int i =1 ;i<grid[0 ].size ();i++){
17+ top += grid[0 ][i];
18+ }
19+
20+ for (int i =0 ;i<grid[0 ].size ();i++){
21+ // All the top points 2nd robot can collect
22+ top -= grid[0 ][i];
23+
24+ // min because first robot wants to minimize
25+ answer = min (answer,max (top,bottom));
26+
27+ // All the bootom points the robot could collect
28+ bottom += grid[1 ][i];
29+ }
30+
31+ return answer;
32+ }
33+ };
You can’t perform that action at this time.
0 commit comments