   思路: 第一个机器人移动的路径是一个 一丨一(像Z),所以第二个机器人想要收集的点数最多,也就是取右上方和坐下方的较大值,即:第一个机器人找出一个下标index,使得max(grid[1][index]左边的和,grid[0][index]右边的和)最小。
class Solution {
public:
long long gridGame(vector<vector<int>>& grid) {
int n = grid[0].size();
vector<long long> pre(n);
vector<long long> post(n);
post[n - 1] = 0;
pre[0] = 0;
for (int i = n - 2; i >= 0; --i) {
post[i] = post[i + 1] + grid[0][i + 1];
}
for (int i = 1; i < n; ++i) {
pre[i] = pre[i - 1] + grid[1][i - 1];
}
long long m = ~(1LL << 63);
for (int i = 0; i < n; ++i) {
if (max(pre[i], post[i]) < m) {
m = max(pre[i], post[i]);
}
}
return m;
}
};
|