LeetCode 题解工作台

网格游戏

给你一个下标从 0 开始的二维数组 grid ,数组大小为 2 x n ,其中 grid[r][c] 表示矩阵中 (r, c) 位置上的点数。现在有两个机器人正在矩阵上参与一场游戏。 两个机器人初始位置都是 (0, 0) ,目标位置是 (1, n-1) 。每个机器人只会 向右 ( (r, c) 到 …

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·matrix

bolt

答案摘要

我们注意到,如果确定了第一个机器人拐头向下的位置 ,那么第二个机器人的最优路径也就确定了,第二个机器人的最优路径就是第一行从 到 的前缀和,或者第二行从 到 的前缀和,取两者的最大值。 我们先计算第一行的后缀点数和,记为 ,第二行的前缀点数和记为 ,初始时 $s_1 = \sum_{j=0}^{n-1} grid[0][j]$, $s_2 = 0$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·matrix 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的二维数组 grid ,数组大小为 2 x n ,其中 grid[r][c] 表示矩阵中 (r, c) 位置上的点数。现在有两个机器人正在矩阵上参与一场游戏。

两个机器人初始位置都是 (0, 0) ,目标位置是 (1, n-1) 。每个机器人只会 向右 ((r, c)(r, c + 1)) 或 向下 ((r, c)(r + 1, c)) 。

游戏开始,第一个 机器人从 (0, 0) 移动到 (1, n-1) ,并收集路径上单元格的全部点数。对于路径上所有单元格 (r, c) ,途经后 grid[r][c] 会重置为 0 。然后,第二个 机器人从 (0, 0) 移动到 (1, n-1) ,同样收集路径上单元的全部点数。注意,它们的路径可能会存在相交的部分。

第一个 机器人想要打击竞争对手,使 第二个 机器人收集到的点数 最小化 。与此相对,第二个 机器人想要 最大化 自己收集到的点数。两个机器人都发挥出自己的 最佳水平 的前提下,返回 第二个 机器人收集到的 点数

 

示例 1:

输入:grid = [[2,5,4],[1,5,1]]
输出:4
解释:第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。
第一个机器人访问过的单元格将会重置为 0 。
第二个机器人将会收集到 0 + 0 + 4 + 0 = 4 个点。

示例 2:

输入:grid = [[3,3,1],[8,5,2]]
输出:4
解释:第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。 
第一个机器人访问过的单元格将会重置为 0 。
第二个机器人将会收集到 0 + 3 + 1 + 0 = 4 个点。

示例 3:

输入:grid = [[1,3,1,15],[1,3,3,1]]
输出:7
解释:第一个机器人的最佳路径如红色所示,第二个机器人的最佳路径如蓝色所示。
第一个机器人访问过的单元格将会重置为 0 。
第二个机器人将会收集到 0 + 1 + 3 + 3 + 0 = 7 个点。

 

提示:

  • grid.length == 2
  • n == grid[r].length
  • 1 <= n <= 5 * 104
  • 1 <= grid[r][c] <= 105
lightbulb

解题思路

方法一:前缀和

我们注意到,如果确定了第一个机器人拐头向下的位置 jj,那么第二个机器人的最优路径也就确定了,第二个机器人的最优路径就是第一行从 j+1j+1n1n-1 的前缀和,或者第二行从 00j1j-1 的前缀和,取两者的最大值。

我们先计算第一行的后缀点数和,记为 s1s_1,第二行的前缀点数和记为 s2s_2,初始时 s1=j=0n1grid[0][j]s_1 = \sum_{j=0}^{n-1} grid[0][j], s2=0s_2 = 0

然后我们枚举第一个机器人拐头向下的位置 jj,此时更新 s1=s1grid[0][j]s_1 = s_1 - grid[0][j], 那么第二个机器人的最优路径和就是 max(s1,s2)max(s_1, s_2),我们取所有 jj 对应的 max(s1,s2)max(s_1, s_2) 的最小值即可。然后更新 s2=s2+grid[1][j]s_2 = s_2 + grid[1][j]

枚举结束后,返回答案即可。

时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。其中 nn 是网格的列数。

1
2
3
4
5
6
7
8
9
10
class Solution:
    def gridGame(self, grid: List[List[int]]) -> int:
        ans = inf
        s1, s2 = sum(grid[0]), 0
        for j, v in enumerate(grid[0]):
            s1 -= v
            ans = min(ans, max(s1, s2))
            s2 += grid[1][j]
        return ans
speed

复杂度分析

指标
时间O(n)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    Focus on the interaction between the first and second robots' paths to optimize the second robot's collection of points.

  • question_mark

    Examine the trade-offs in choosing paths for the second robot, especially when paths overlap with the first robot's path.

  • question_mark

    The ability to implement efficient algorithms in linear time will be key to solving this problem within the constraints.

warning

常见陷阱

外企场景
  • error

    Forgetting to account for the cells visited by the first robot, which are marked as 0 and affect the second robot's path.

  • error

    Failing to optimize the second robot's path by greedily choosing the best option at each step based on updated matrix values.

  • error

    Not handling the matrix's large size properly within the time limits, leading to inefficient solutions with higher time complexity.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increase the grid size beyond 2 rows and optimize for larger matrixes.

  • arrow_right_alt

    Implementing additional constraints, like diagonal movements or limited robot movement options.

  • arrow_right_alt

    Modifying the game to include dynamic changes in point values during the game, requiring continuous re-evaluation of optimal paths.

help

常见问题

外企场景

网格游戏题解:数组·matrix | LeetCode #2017 中等