LeetCode 题解工作台
摘樱桃 II
给你一个 rows x cols 的矩阵 grid 来表示一块樱桃地。 grid 中每个格子的数字表示你能获得的樱桃数目。 你有两个机器人帮你收集樱桃,机器人 1 从左上角格子 (0,0) 出发,机器人 2 从右上角格子 (0, cols-1) 出发。 请你按照如下规则,返回两个机器人能收集的最多樱…
3
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们定义 表示两个机器人分别在第 行的位置 和 时能够摘到的最多樱桃数目。初始时 $f[0][0][n-1] = grid[0][0] + grid[0][n-1]$,其余值均为 。答案为 $\max_{0 \leq j_1, j_2 < n} f[m-1][j_1][j_2]$。 考虑 ,如果 $j_1 \neq j_2$,那么机器人在第 行能摘到的樱桃数目为 $grid[i][j_1…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你一个 rows x cols 的矩阵 grid 来表示一块樱桃地。 grid 中每个格子的数字表示你能获得的樱桃数目。
你有两个机器人帮你收集樱桃,机器人 1 从左上角格子 (0,0) 出发,机器人 2 从右上角格子 (0, cols-1) 出发。
请你按照如下规则,返回两个机器人能收集的最多樱桃数目:
- 从格子
(i,j)出发,机器人可以移动到格子(i+1, j-1),(i+1, j)或者(i+1, j+1)。 - 当一个机器人经过某个格子时,它会把该格子内所有的樱桃都摘走,然后这个位置会变成空格子,即没有樱桃的格子。
- 当两个机器人同时到达同一个格子时,它们中只有一个可以摘到樱桃。
- 两个机器人在任意时刻都不能移动到
grid外面。 - 两个机器人最后都要到达
grid最底下一行。
示例 1:

输入:grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] 输出:24 解释:机器人 1 和机器人 2 的路径在上图中分别用绿色和蓝色表示。 机器人 1 摘的樱桃数目为 (3 + 2 + 5 + 2) = 12 。 机器人 2 摘的樱桃数目为 (1 + 5 + 5 + 1) = 12 。 樱桃总数为: 12 + 12 = 24 。
示例 2:

输入:grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]] 输出:28 解释:机器人 1 和机器人 2 的路径在上图中分别用绿色和蓝色表示。 机器人 1 摘的樱桃数目为 (1 + 9 + 5 + 2) = 17 。 机器人 2 摘的樱桃数目为 (1 + 3 + 4 + 3) = 11 。 樱桃总数为: 17 + 11 = 28 。
示例 3:
输入:grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] 输出:22
示例 4:
输入:grid = [[1,1],[1,1]] 输出:4
提示:
rows == grid.lengthcols == grid[i].length2 <= rows, cols <= 700 <= grid[i][j] <= 100
解题思路
方法一:动态规划
我们定义 表示两个机器人分别在第 行的位置 和 时能够摘到的最多樱桃数目。初始时 ,其余值均为 。答案为 。
考虑 ,如果 ,那么机器人在第 行能摘到的樱桃数目为 ;如果 ,那么机器人在第 行能摘到的樱桃数目为 。我们可以枚举两个机器人的上一个状态 ,其中 分别是两个机器人在第 行的位置,那么有 且 。状态转移方程如下:
其中 为 时需要忽略。
最终的答案即为 。
时间复杂度 ,空间复杂度 。其中 和 分别是网格的行数和列数。
class Solution:
def cherryPickup(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
f = [[[-1] * n for _ in range(n)] for _ in range(m)]
f[0][0][n - 1] = grid[0][0] + grid[0][n - 1]
for i in range(1, m):
for j1 in range(n):
for j2 in range(n):
x = grid[i][j1] + (0 if j1 == j2 else grid[i][j2])
for y1 in range(j1 - 1, j1 + 2):
for y2 in range(j2 - 1, j2 + 2):
if 0 <= y1 < n and 0 <= y2 < n and f[i - 1][y1][y2] != -1:
f[i][j1][j2] = max(f[i][j1][j2], f[i - 1][y1][y2] + x)
return max(f[-1][j1][j2] for j1, j2 in product(range(n), range(n)))
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(rows * cols^2 * 9) because for each row and pair of robot positions, there are 3x3 next moves. Space complexity is O(rows * cols^2) for the DP array. Optimizations can reduce it to O(cols^2) per row. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Checking if you consider both robots simultaneously and handle overlapping cells.
- question_mark
Watching if DP is defined correctly to avoid recomputation and ensure transitions cover all next moves.
- question_mark
Observing whether you can reduce space from full 3D to 2D rolling array optimization.
常见陷阱
外企场景- error
Counting cherries twice when both robots occupy the same cell.
- error
Using a 2D DP ignoring the second robot's position, leading to incorrect totals.
- error
Failing to iterate all 3x3 move combinations for the next row, missing optimal paths.
进阶变体
外企场景- arrow_right_alt
Single robot cherry pickup with only one path, simpler 2D DP suffices.
- arrow_right_alt
Allow diagonal jumps longer than one cell, requiring extended DP transitions.
- arrow_right_alt
Grid with obstacles where some cells cannot be visited, adding conditional checks in DP.