🐦‍🔥🐦‍🔥🐦‍🔥No Shit , Best Easiest Solution❤️❤️‍🔥🔥
IntuitionThe problem is asking to calculate the highest possible elevation for each land cell in a matrix, where the water cells are given a height of 0. The ma
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem is asking to calculate the highest possible elevation for each land cell in a matrix, where the water cells are given a height of 0. The main idea is to perform a breadth-first search (BFS) from all the water cells simultaneously to compute the height of the neighboring land cells. The land cell's height will be 1 more than its neighboring water cell's height. This allows us to propagate the height values outward from the water cells to all the land cells in an optimal way.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Matrix Setup: Create a matrix where each cell is initially marked as -1 (indicating that the height has not been calculated yet). The water cells (with a value of 1 in the input matrix) will be assigned a height of 0 and added to a queue for BFS.

2. BFS Initialization: Start the BFS with all water cells. For each water cell, we assign it a height of 0 and push it into the BFS queue.

3. BFS Execution: In the BFS loop, for each water cell, we check its four possible neighbors (right, left, down, up). If the neighboring land cell has not been visited (i.e., its value is -1), we assign it a height of one more than the current cell's height and add it to the queue for further exploration.

4. End Condition: The BFS will propagate through the entire matrix, calculating the height for each land cell, ensuring that we always use the shortest path from a water cell to each land cell, giving the highest peak possible.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
   The time complexity is O(m*n) is the number of columns in the matrix. 
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
This is also O(m*n) because we need to store the matrix for heights, and the BFS queue may store up to m*n elements in the worst case (if all cells are water cells initially).

##
##
##

![image.png](https://assets.leetcode.com/users/images/e48be2c2-d22f-4054-8e57-837215347f9f_1737570506.3289225.png)

# Code
```python3 []
class Solution:
    def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
        m, n = len(isWater), len(isWater[0])
        matrix = [[-1] * n for _ in range(m)]
        que = deque()

        for i in range(m):
            for j in range(n):
                if isWater[i][j] == 1:
                    matrix[i][j] = 0
                    que.append((i, j))

        directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]

        while que:
            r, c = que.popleft()

            for dr, dc in directions:
                nr, nc = r + dr, c + dc
                if 0 <= nr < m and 0 <= nc < n and matrix[nr][nc] == -1:
                    matrix[nr][nc] = matrix[r][c] + 1
                    que.append((nr, nc))

        return matrix

```
```C++ []
class Solution {
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int m = isWater.size();    // Number of rows
        int n = isWater[0].size(); // Number of columns
        
        // Initialize the result matrix with -1 (indicating unvisited land cells)
        vector<vector<int>> matrix(m, vector<int>(n, -1));
        
        // Queue to perform BFS
        queue<pair<int, int>> q;

        // Initialize the matrix with water cells having height 0
        // and add them to the queue for BFS
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (isWater[i][j] == 1) {
                    matrix[i][j] = 0;  // Water cells have height 0
                    q.push({i, j});     // Add the water cells to the queue
                }
            }
        }

        // Directions for moving in 4 possible directions (right, left, down, up)
        vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

        // Perform BFS to calculate the height for each land cell
        while (!q.empty()) {
            // Get the front element from the queue
            auto [r, c] = q.front();
            q.pop();

            // Explore all 4 directions (neighbors)
            for (auto& dir : directions) {
                int nr = r + dir.first; // New row
                int nc = c + dir.second; // New column

                // Check if the new position is within bounds and is a land cell (-1)
                if (nr >= 0 && nr < m && nc >= 0 && nc < n && matrix[nr][nc] == -1) {
                    // Set the height of the neighboring land cell to 1 more than the current cell's height
                    matrix[nr][nc] = matrix[r][c] + 1;
                    // Add the neighboring cell to the queue for further exploration
                    q.push({nr, nc});
                }
            }
        }

        return matrix; // Return the resulting matrix with heights
    }
};


```
```Java []
class Solution {
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length;    // Number of rows
        int n = isWater[0].length; // Number of columns
        
        // Initialize the result matrix with -1 (indicating unvisited land cells)
        int[][] matrix = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = -1; // Mark all cells as land initially
            }
        }

        // Queue to perform BFS (Breadth-First Search)
        Queue<int[]> queue = new LinkedList<>();

        // Initialize the matrix with water cells having height 0
        // and add them to the queue for BFS
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (isWater[i][j] == 1) {
                    matrix[i][j] = 0;  // Water cells have height 0
                    queue.add(new int[]{i, j}); // Add water cells to queue
                }
            }
        }

        // Directions for moving in 4 possible directions (right, left, down, up)
        int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

        // Perform BFS to calculate the height for each land cell
        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int r = current[0];
            int c = current[1];

            // Explore all 4 directions (neighbors)
            for (int[] dir : directions) {
                int nr = r + dir[0]; // New row
                int nc = c + dir[1]; // New column

                // Check if the new position is within bounds and is a land cell (-1)
                if (nr >= 0 && nr < m && nc >= 0 && nc < n && matrix[nr][nc] == -1) {
                    // Set the height of the neighboring land cell to 1 more than the current cell's height
                    matrix[nr][nc] = matrix[r][c] + 1;
                    // Add the neighboring cell to the queue for further exploration
                    queue.add(new int[]{nr, nc});
                }
            }
        }

        return matrix; // Return the resulting matrix with heights
    }
}
```
![image.png](https://assets.leetcode.com/users/images/1c9818b0-1638-4a50-b5da-a30075418d7b_1737570595.426308.png)![image.png](https://assets.leetcode.com/users/images/1c9818b0-1638-4a50-b5da-a30075418d7b_1737570595.426308.png)

['Breadth-First Search', 'C++', 'Java', 'Python3']