Using BFS | Optimal | Clearest | Most Efficient | Python Approach
IntuitionThe goal is to assign heights to land cells based on their shortest distance to the nearest water cell. The key observation here is that water cells (i
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The goal is to assign heights to land cells based on their shortest distance to the nearest water cell. The key observation here is that water cells (isWater[i][j] = 1) have a height of 0, and land cells (isWater[i][j] = 0) should have a height that increases as we move away from water.

To efficiently assign these heights, we use Breadth-First Search (BFS), which naturally expands in a wave-like manner from the starting points (water cells). This guarantees that we assign the correct shortest distance to each land cell.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize Data Structures:

- *direction* stores the four possible movement directions (up, down, left, right).
- *m* and *n* store the dimensions of the grid.
- *height* is initialized to -1 everywhere to track unvisited cells.
- *queue* (a deque) stores all **water cells** (isWater[row][col] == 1), setting their *height* to 0.
2. BFS Processing:

- While the *queue* is not empty, remove the front cell.
- Check all four possible adjacent cells.
- If an adjacent cell **has not been visited** (height[r][c] == -1), update its *height* to height[row][col] + 1 and push it into the queue.
- Return the *height* matrix, which now contains the correct *height* for each land cell.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
    - O(m x n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
    - O(m x n)
# Code
```python3 []
class Solution:
    def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
        
        direction = [(1,0),(-1,0),(0,1),(0,-1)]
        m = len(isWater)
        n = len(isWater[0])
        height = [[-1]*n for _ in range(m)]
        queue = deque()
        for row in range(m):
            for col in range(n):
                
                if isWater[row][col] == 1:
                    queue.append((row,col))
                    height[row][col] = 0

        while queue:
            row,col = queue.popleft()
            for dx,dy in direction:
                r = row + dx
                c = col + dy
                if 0<=r<m and 0<=c<n and height[r][c] == -1:
                    height[r][c] = height[row][col]+1
                    queue.append((r,c))
                    # print(queue)
        return height
```
['Breadth-First Search', 'Python3']