You are given an integer matrix isWater
of size m x n
that represents a map of land and water cells.
isWater[i][j] == 0
, cell (i, j)
is a land cell.isWater[i][j] == 1
, cell (i, j)
is a water cell.You must assign each cell a height in a way that follows these rules:
0
.1
. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).Find an assignment of heights such that the maximum height in the matrix is maximized.
Return an integer matrix height
of size m x n
where height[i][j]
is cell (i, j)
's height. If there are multiple solutions, return any of them.
Example 1:
Input: isWater = [[0,1],[0,0]] Output: [[1,0],[2,1]] Explanation: The image shows the assigned heights of each cell. The blue cell is the water cell, and the green cells are the land cells.
Example 2:
Input: isWater = [[0,0,1],[1,0,0],[0,0,0]] Output: [[1,1,0],[0,1,1],[1,2,2]] Explanation: A height of 2 is the maximum possible height of any assignment. Any height assignment that has a maximum height of 2 while still meeting the rules will also be accepted.
Constraints:
m == isWater.length
n == isWater[i].length
1 <= m, n <= 1000
isWater[i][j]
is 0
or 1
.
Note: This question is the same as 542: https://leetcode.com/problems/01-matrix/
The problem asks to assign heights to cells in a matrix representing land and water, such that:
The optimal solution involves a Breadth-First Search (BFS) algorithm. The idea is to start from all water cells (height 0) and expand outwards, incrementing the height by 1 for each adjacent land cell. This guarantees that the height difference between adjacent cells is at most 1, and the maximum height is maximized because we explore all possible heights in a level-order manner.
Initialization: Create a result matrix ans
initialized with -1 for land cells and 0 for water cells. Use a queue q
to store the coordinates of water cells.
BFS: While the queue is not empty:
(i, j)
.ans
) and within the matrix bounds:
ans[i][j] + 1
.Return: Return the ans
matrix.
ans
and the queue q
which could hold all the cells if the entire map is land. In the best case (only water cells), space complexity is O(W), where W is the number of water cells.The code implementations provided use slightly different approaches to handle the queue and neighbor traversal, but all follow the core BFS algorithm. The main difference between Solution 1 and Solution 2 lies in how the queue is processed in each iteration. Solution 1 processes one cell at a time, while Solution 2 processes all cells added in the previous level before moving to the next level. Both solutions achieve the same result, but Solution 2 might be slightly more efficient in practice as it avoids unnecessary queue operations. The choice between these two solutions is often a matter of personal preference or specific optimization needs. The key is that the BFS structure remains consistent in both.
Each code snippet provided shows the implementation in Python, Java, C++, Go, TypeScript, and Rust, all showcasing the BFS approach effectively. The comments within the code enhance readability and understanding. The pairwise
function in the Python solution (used for efficient neighbor traversal) can be easily implemented or replaced with explicit neighbor checks in other languages.