-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsolution.js
31 lines (27 loc) · 833 Bytes
/
solution.js
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
/**
* @param {number[][]} grid
* @return {number}
*/
var gridGame = function (grid) {
const n = grid[0].length;
const topSuffix = Array(n).fill(0);
const bottomPrefix = Array(n).fill(0);
// Calculate suffix sum for the top row
topSuffix[n - 1] = grid[0][n - 1];
for (let i = n - 2; i >= 0; --i) {
topSuffix[i] = topSuffix[i + 1] + grid[0][i];
}
// Calculate prefix sum for the bottom row
bottomPrefix[0] = grid[1][0];
for (let i = 1; i < n; ++i) {
bottomPrefix[i] = bottomPrefix[i - 1] + grid[1][i];
}
// Find the minimum maximum points Robot 2 can collect
let result = Infinity;
for (let i = 0; i < n; ++i) {
const top = i + 1 < n ? topSuffix[i + 1] : 0;
const bottom = i > 0 ? bottomPrefix[i - 1] : 0;
result = Math.min(result, Math.max(top, bottom));
}
return result;
};