 ✅ Map of Highest Peak | JS🔥 | beats 99%🚀 | 🚀highly optimised & easy to understand ✅
Explanation
The algorithm uses Breadth-First Search (BFS) to calculate the heights of all land cells while adhering to the constraints:
Water cells (isWater[i][
# Explanation
- The algorithm uses Breadth-First Search (BFS) to calculate the heights of all land cells while adhering to the constraints:
- Water cells (isWater[i][j] === 1) are initialized to height 0 and serve as the starting points for BFS.
- Land cells are filled with increasing heights, ensuring adjacent cells have height differences of at most 1.

#### Steps in the Algorithm
> **Initialization:**
- Create a height matrix with dimensions m×n, initialized to -1 (unvisited).
- Add all water cells to a queue with height 0 and mark them as visited in the height matrix.
> **BFS Traversal:**
- Process each cell in the queue starting from the water cells.
- For each cell, check its four neighbors (north, east, south, west). If a neighbor is unvisited (height[nx][ny] === -1), assign it a height of current_height + 1 and add it to the queue.
> **Avoid Redundancy:**
- Use a simple index-based queue processing instead of shift() to minimize overhead in the queue operations.
> **Return the Result:**
- After BFS completes, the height matrix contains the height of all cells.

# Complexity
> **Time complexity:**
- Each cell in the matrix is visited exactly once, resulting in O(m×n) operations.
- For each cell, at most 4 neighbors are checked, which is constant for a given cell.
- Overall BFS cost: O(m×n).
- Total Time Complexity: O(m×n)
> **Space complexity:**
- The height matrix requires O(m×n) space.
- In the worst case (e.g., all water cells surrounding land cells), the queue size can grow to O(m×n).
- Total Space Complexity: O(m×n)
# Code
```javascript []
/**
 * @param {number[][]} isWater
 * @return {number[][]}
 */
var highestPeak = function (isWater) {
    const m = isWater.length;
    const n = isWater[0].length;

    const directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // Right, Down, Left, Up
    const height = Array.from({ length: m }, () => Array(n).fill(-1)); // Initialize all cells to -1
    const queue = [];

    // Initialize the queue with water cells and mark them in the height array
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (isWater[i][j] === 1) {
                height[i][j] = 0; // Water cells have height 0
                queue.push([i, j]); // Add water cells to the queue
            }
        }
    }

    // Perform BFS
    let index = 0; // Avoid frequent queue shifting by using an index
    while (index < queue.length) {
        const [x, y] = queue[index];
        index++;

        for (const [dx, dy] of directions) {
            const nx = x + dx;
            const ny = y + dy;

            // If the cell is within bounds and not visited
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && height[nx][ny] === -1) {
                height[nx][ny] = height[x][y] + 1; // Set height
                queue.push([nx, ny]); // Add the cell to the queue
            }
        }
    }

    return height;
};


```


![DALL·E 2025-01-21 10.20.30 - A realistic, soft-toned image of a kind and humble person, sitting at a desk with a gentle smile, holding a sign that says_ 'Please upvote if this hel.webp](https://assets.leetcode.com/users/images/055596b9-e829-4c4a-aac0-6c6b2975f910_1737511628.1747005.webp)

['JavaScript']