LeetCode 题解工作台

地图中的最高点

给你一个大小为 m x n 的整数矩阵 isWater ,它代表了一个由 陆地 和 水域 单元格组成的地图。 如果 isWater[i][j] == 0 ,格子 (i, j) 是一个 陆地 格子。 如果 isWater[i][j] == 1 ,格子 (i, j) 是一个 水域 格子。 你需要按照如下…

category

3

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

中等 · 图·搜索

bolt

答案摘要

根据题目描述,水域的高度必须是 ,而任意相邻格子的高度差至多为 。因此,我们可以从所有水域格子出发,用 BFS 搜索相邻且未访问过的格子,将其高度置为当前格子的高度再加一。 最后返回结果矩阵即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 图·搜索 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个大小为 m x n 的整数矩阵 isWater ,它代表了一个由 陆地 和 水域 单元格组成的地图。

  • 如果 isWater[i][j] == 0 ,格子 (i, j) 是一个 陆地 格子。
  • 如果 isWater[i][j] == 1 ,格子 (i, j) 是一个 水域 格子。

你需要按照如下规则给每个单元格安排高度:

  • 每个格子的高度都必须是非负的。
  • 如果一个格子是 水域 ,那么它的高度必须为 0 。
  • 任意相邻的格子高度差 至多 为 1 。当两个格子在正东、南、西、北方向上相互紧挨着,就称它们为相邻的格子。(也就是说它们有一条公共边)

找到一种安排高度的方案,使得矩阵中的最高高度值 最大 。

请你返回一个大小为 m x n 的整数矩阵 height ,其中 height[i][j] 是格子 (i, j) 的高度。如果有多种解法,请返回 任意一个 。

 

示例 1:

输入:isWater = [[0,1],[0,0]]
输出:[[1,0],[2,1]]
解释:上图展示了给各个格子安排的高度。
蓝色格子是水域格,绿色格子是陆地格。

示例 2:

输入:isWater = [[0,0,1],[1,0,0],[0,0,0]]
输出:[[1,1,0],[0,1,1],[1,2,2]]
解释:所有安排方案中,最高可行高度为 2 。
任意安排方案中,只要最高高度为 2 且符合上述规则的,都为可行方案。

 

提示:

  • m == isWater.length
  • n == isWater[i].length
  • 1 <= m, n <= 1000
  • isWater[i][j] 要么是 0 ,要么是 1 。
  • 至少有 1 个水域格子。
注意:本题与 542 题相同。
lightbulb

解题思路

方法一:BFS

根据题目描述,水域的高度必须是 00,而任意相邻格子的高度差至多为 11。因此,我们可以从所有水域格子出发,用 BFS 搜索相邻且未访问过的格子,将其高度置为当前格子的高度再加一。

最后返回结果矩阵即可。

时间复杂度 O(m×n)O(m \times n),空间复杂度 O(m×n)O(m \times n)。其中 mmnn 分别是整数矩阵 isWater 的行数和列数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
        m, n = len(isWater), len(isWater[0])
        ans = [[-1] * n for _ in range(m)]
        q = deque()
        for i, row in enumerate(isWater):
            for j, v in enumerate(row):
                if v:
                    q.append((i, j))
                    ans[i][j] = 0
        while q:
            i, j = q.popleft()
            for a, b in pairwise((-1, 0, 1, 0, -1)):
                x, y = i + a, j + b
                if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
                    ans[x][y] = ans[i][j] + 1
                    q.append((x, y))
        return ans
speed

复杂度分析

指标
时间O(m \times n)
空间O(m \times n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Check if the candidate properly utilizes BFS to traverse the matrix and propagate height values from water cells.

  • question_mark

    Look for a clear explanation of how the BFS approach ensures that the maximum height is achieved.

  • question_mark

    Verify that the candidate discusses the queue management and how it helps to efficiently process the matrix.

warning

常见陷阱

外企场景
  • error

    Incorrectly updating the heights of land cells, leading to invalid height propagation.

  • error

    Not using BFS to propagate the heights from all water cells simultaneously, which may result in suboptimal solutions.

  • error

    Overcomplicating the solution by using unnecessary algorithms or operations instead of focusing on BFS.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Vary the size of the matrix to test the efficiency of the BFS approach.

  • arrow_right_alt

    Introduce more complex terrain with varying water distributions, requiring careful height propagation.

  • arrow_right_alt

    Add constraints such as larger grid sizes or specific height boundaries to test edge cases and algorithm efficiency.

help

常见问题

外企场景

地图中的最高点题解:图·搜索 | LeetCode #1765 中等