Easy Solution || JAVA || BFS
IntuitionThe problem requires calculating the height of each land cell such that its value represents the minimum distance to a water cell. This can be efficien
# Intuition
The problem requires calculating the height of each land cell such that its value represents the minimum distance to a water cell. This can be efficiently solved using Breadth-First Search (BFS), starting from all water cells simultaneously.
# Approach
Initialization:

Create a heights matrix to store the final heights, initialized to -1 for all land cells.
Use a boolean visited matrix to ensure each cell is processed only once.
Push all water cells (height 0) into a queue as starting points for the BFS.
BFS Traversal:

Use a queue to perform BFS. Each water cell spreads its height to its unvisited neighboring land cells.
For each cell dequeued, calculate the height for its neighbors as current height + 1 and enqueue these neighbors.
Direction Handling:

Use direction vectors (deltaRow and deltaCol) to traverse the grid in four possible directions (up, down, left, right).
Output:

After BFS completes, the heights matrix contains the desired height values for all cells.

# Complexity
- Time complexity:
O(rows×cols)
Each cell is visited once, and all neighbors are checked.

- Space complexity:
O(rows×cols)
The queue can hold at most all cells, and additional space is used for the visited matrix.

# Code
```java []
import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[][] highestPeak(int[][] isWater) {
        int rows = isWater.length;
        int cols = isWater[0].length;

        // Result matrix to store heights
        int[][] heights = new int[rows][cols];

        // Visited matrix to track processed cells
        boolean[][] visited = new boolean[rows][cols];

        // Queue for BFS
        Queue<Cell> queue = new LinkedList<>();

        // Initialize the queue with all water cells
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (isWater[row][col] == 1) {
                    queue.add(new Cell(row, col, 0)); // Add water cell with height 0
                    heights[row][col] = 0; // Water cells have height 0
                    visited[row][col] = true; // Mark as visited
                } else {
                    heights[row][col] = -1; // Placeholder for land cells
                }
            }
        }

        // Direction vectors for moving up, down, left, right
        int[] deltaRow = {-1, 0, 1, 0};
        int[] deltaCol = {0, 1, 0, -1};

        // Perform BFS
        while (!queue.isEmpty()) {
            Cell current = queue.poll();
            int row = current.row;
            int col = current.col;
            int height = current.height;

            // Traverse all 4 neighboring cells
            for (int direction = 0; direction < 4; direction++) {
                int newRow = row + deltaRow[direction];
                int newCol = col + deltaCol[direction];

                // Check if the neighbor is within bounds and unvisited
                if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && !visited[newRow][newCol]) {
                    heights[newRow][newCol] = height + 1; // Update height
                    queue.add(new Cell(newRow, newCol, height + 1)); // Add to queue
                    visited[newRow][newCol] = true; // Mark as visited
                }
            }
        }

        return heights;
    }
}

// Custom Cell class to store row, column, and height
class Cell {
    int row, col, height;

    Cell(int row, int col, int height) {
        this.row = row;
        this.col = col;
        this.height = height;
    }
}

```
['Array', 'Breadth-First Search', 'Matrix', 'Java']