LeetCode 题解工作台

球会落何处

用一个大小为 m x n 的二维网格 grid 表示一个箱子。你有 n 颗球。箱子的顶部和底部都是开着的。 箱子中的每个单元格都有一个对角线挡板,跨过单元格的两个角,可以将球导向左侧或者右侧。 将球导向右侧的挡板跨过左上角和右下角,在网格中用 1 表示。 将球导向左侧的挡板跨过右上角和左下角,在网格…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·matrix

bolt

答案摘要

我们可以使用 DFS 来模拟球的运动过程,设计一个函数 $\textit{dfs}(i, j)$,表示球从第 行第 列出发,最终会落在第几列。对于以下情况,球会卡住: 1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧

Interview AiBox logo

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

试试 AiBox 面试助手arrow_forward
description

题目描述

用一个大小为 m x n 的二维网格 grid 表示一个箱子。你有 n 颗球。箱子的顶部和底部都是开着的。

箱子中的每个单元格都有一个对角线挡板,跨过单元格的两个角,可以将球导向左侧或者右侧。

  • 将球导向右侧的挡板跨过左上角和右下角,在网格中用 1 表示。
  • 将球导向左侧的挡板跨过右上角和左下角,在网格中用 -1 表示。

在箱子每一列的顶端各放一颗球。每颗球都可能卡在箱子里或从底部掉出来。如果球恰好卡在两块挡板之间的 "V" 形图案,或者被一块挡导向到箱子的任意一侧边上,就会卡住。

返回一个大小为 n 的数组 answer ,其中 answer[i] 是球放在顶部的第 i 列后从底部掉出来的那一列对应的下标,如果球卡在盒子里,则返回 -1

 

示例 1:

输入:grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
输出:[1,-1,-1,-1,-1]
解释:示例如图:
b0 球开始放在第 0 列上,最终从箱子底部第 1 列掉出。
b1 球开始放在第 1 列上,会卡在第 2、3 列和第 1 行之间的 "V" 形里。
b2 球开始放在第 2 列上,会卡在第 2、3 列和第 0 行之间的 "V" 形里。
b3 球开始放在第 3 列上,会卡在第 2、3 列和第 0 行之间的 "V" 形里。
b4 球开始放在第 4 列上,会卡在第 2、3 列和第 1 行之间的 "V" 形里。

示例 2:

输入:grid = [[-1]]
输出:[-1]
解释:球被卡在箱子左侧边上。

示例 3:

输入:grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
输出:[0,1,2,3,4,-1]

 

提示:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 100
  • grid[i][j]1-1
lightbulb

解题思路

方法一:分情况讨论 + DFS

我们可以使用 DFS 来模拟球的运动过程,设计一个函数 dfs(i,j)\textit{dfs}(i, j),表示球从第 ii 行第 jj 列出发,最终会落在第几列。对于以下情况,球会卡住:

  1. 球位于最左一列,并且球所在的单元格单元格挡板将球导向左侧
  2. 球位于最右一列,并且此单元格挡板将球导向右侧
  3. 球所在的单元格挡板将球导向右侧,并且球右侧相邻单元格挡板将球导向左侧
  4. 球所在的单元格挡板将球导向左侧,并且球左侧相邻单元格挡板将球导向右侧

如果满足以上任意一种情况,我们就可以判断球会卡住,返回 1-1。否则,我们就可以继续递归地寻找球的下一个位置。最后,如果球到了最后一行,我们就可以返回当前列的编号。

时间复杂度 O(m×n)O(m \times n),空间复杂度 O(m)O(m)。其中 mmnn 分别是数组 grid\textit{grid} 的行数和列数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
    def findBall(self, grid: List[List[int]]) -> List[int]:
        def dfs(i: int, j: int) -> int:
            if i == m:
                return j
            if j == 0 and grid[i][j] == -1:
                return -1
            if j == n - 1 and grid[i][j] == 1:
                return -1
            if grid[i][j] == 1 and grid[i][j + 1] == -1:
                return -1
            if grid[i][j] == -1 and grid[i][j - 1] == 1:
                return -1
            return dfs(i + 1, j + 1) if grid[i][j] == 1 else dfs(i + 1, j - 1)

        m, n = len(grid), len(grid[0])
        return [dfs(0, j) for j in range(n)]
speed

复杂度分析

指标
时间complexity is O(m * n) since each ball may traverse all rows in its column. Space complexity is O(n) for storing the output array, or O(m) additional if using recursion in DFS for path tracking.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Notice each ball moves independently through the grid; look for per-column simulation patterns.

  • question_mark

    Pay attention to the 'V' shaped stuck condition formed by adjacent cells and edge cases on walls.

  • question_mark

    Consider iterative versus recursive approaches to clearly handle ball paths and stuck detection.

warning

常见陷阱

外企场景
  • error

    Forgetting to check the 'V' shaped block between two adjacent boards leading to incorrect stuck results.

  • error

    Ignoring wall boundaries causing array index errors or wrong exit positions.

  • error

    Mixing row and column indices, which can misdirect the ball in the simulation.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return only the count of balls that successfully exit rather than their positions.

  • arrow_right_alt

    Allow arbitrary board angles represented by integers beyond 1 and -1, requiring generalized movement rules.

  • arrow_right_alt

    Simulate gravity in reverse from bottom to top to compute reachable top columns.

help

常见问题

外企场景

球会落何处题解:数组·matrix | LeetCode #1706 中等