-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2017-grid-game.java
30 lines (30 loc) · 935 Bytes
/
2017-grid-game.java
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
class Solution {
public long gridGame(int[][] grid) {
long[] prefix_top = new long[grid[0].length];
long[] prefix_bottom = new long[grid[0].length];
for(int i = 0; i < grid[0].length; i++)
{
prefix_top[i] = grid[0][i];
prefix_bottom[i] = grid[1][i];
}
for(int i = 1; i < grid[0].length; i++)
{
prefix_top[i] += prefix_top[i - 1];
prefix_bottom[i] += prefix_bottom[i - 1];
}
long res = Long.MAX_VALUE;
long maxi = Long.MIN_VALUE;
for(int i = 0; i < grid[0].length; i++)
{
long top = prefix_top[grid[0].length - 1] - prefix_top[i];
long bottom = 0;
if(i > 0)
{
bottom = prefix_bottom[i - 1];
}
maxi = Math.max(top, bottom);
res = Math.min(res, maxi);
}
return res;
}
}