🌟BFS Solution Simplified ✨ | Beginner-Friendly 🚀 | Code & Explanation 💻
Intuition 🧠💧🏔️Water cells (🌊) are the starting point with height 0. Heights of land cells (🏞️) increase the farther they are from water. Imagine ripples spreadi
# Intuition 🧠💧🏔️
Water cells (🌊) are the starting point with height 0. Heights of land cells (🏞️) increase the farther they are from water. Imagine ripples spreading outward from water cells until all land cells are covered.

---

# Approach 🛠️⚙️
1. Initialization:

    - Traverse the grid: Mark water cells as 0 and add them to a queue. Mark land cells as -1 (unvisited).

2. Breadth-First Search (BFS):

3. While the queue is not empty, process each cell:
    - Check all 4 neighboring cells (👆👇👉👈).
    - If a neighbor is unvisited (-1), update its height to the current cell’s height + 1 and add it to the queue.
4. Completion:

    - Continue until all land cells have heights assigned.

---

# Complexity ⏳💡
## Time Complexity: O(n × m)
     Each cell is processed once, and each edge is considered.
## Space Complexity: O(n × m)
    For the queue and storage of the grid.

---

# Code
```cpp []
class Solution {
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        queue<pair<int, int>> q;
        int n = isWater.size(), m = isWater[0].size();
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(isWater[i][j] == 1) {
                    q.push({i, j});
                    isWater[i][j] = 0;
                } else {
                    isWater[i][j] = -1;
                }
            }
        }

        vector<pair<int, int>> dir = {{0,1}, {1,0}, {0,-1}, {-1,0}};
        while(!q.empty()) {
            auto [x, y] = q.front();
            q.pop();

            for(auto [dx, dy]: dir) {
                int nx = x + dx, ny = y + dy;
                if(nx >= 0 && ny >= 0 && nx < n && ny < m) {
                    if(isWater[nx][ny] == -1) {
                        isWater[nx][ny] = isWater[x][y] + 1;
                        q.push({nx, ny});
                    }
                }
            }
        }
        return isWater;
    }
};
```

```java []
import java.util.*;

class Solution {
    public int[][] highestPeak(int[][] isWater) {
        int n = isWater.length, m = isWater[0].length;
        Queue<int[]> q = new LinkedList<>();
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (isWater[i][j] == 1) {
                    q.offer(new int[]{i, j});
                    isWater[i][j] = 0;
                } else {
                    isWater[i][j] = -1;
                }
            }
        }
        
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        while (!q.isEmpty()) {
            int[] current = q.poll();
            int x = current[0], y = current[1];
            
            for (int[] dir : directions) {
                int nx = x + dir[0], ny = y + dir[1];
                if (nx >= 0 && ny >= 0 && nx < n && ny < m && isWater[nx][ny] == -1) {
                    isWater[nx][ny] = isWater[x][y] + 1;
                    q.offer(new int[]{nx, ny});
                }
            }
        }
        return isWater;
    }
}

```
```javascript []
var highestPeak = function(isWater) {
    let n = isWater.length, m = isWater[0].length;
    let q = [];
    
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            if (isWater[i][j] === 1) {
                q.push([i, j]);
                isWater[i][j] = 0;
            } else {
                isWater[i][j] = -1;
            }
        }
    }
    
    let directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    while (q.length > 0) {
        let [x, y] = q.shift();
        for (let [dx, dy] of directions) {
            let nx = x + dx, ny = y + dy;
            if (nx >= 0 && ny >= 0 && nx < n && ny < m && isWater[nx][ny] === -1) {
                isWater[nx][ny] = isWater[x][y] + 1;
                q.push([nx, ny]);
            }
        }
    }
    return isWater;
};

```
```python []
from collections import deque

class Solution:
    def highestPeak(self, isWater):
        n, m = len(isWater), len(isWater[0])
        q = deque()
        
        for i in range(n):
            for j in range(m):
                if isWater[i][j] == 1:
                    q.append((i, j))
                    isWater[i][j] = 0
                else:
                    isWater[i][j] = -1
        
        directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        while q:
            x, y = q.popleft()
            for dx, dy in directions:
                nx, ny = x + dx, y + dy
                if 0 <= nx < n and 0 <= ny < m and isWater[nx][ny] == -1:
                    isWater[nx][ny] = isWater[x][y] + 1
                    q.append((nx, ny))
        
        return isWater

```
```ruby []
def highest_peak(is_water)
    n = is_water.length
    m = is_water[0].length
    q = []
    
    (0...n).each do |i|
        (0...m).each do |j|
            if is_water[i][j] == 1
                q << [i, j]
                is_water[i][j] = 0
            else
                is_water[i][j] = -1
            end
        end
    end
    
    directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    until q.empty?
        x, y = q.shift
        directions.each do |dx, dy|
            nx, ny = x + dx, y + dy
            if nx.between?(0, n - 1) && ny.between?(0, m - 1) && is_water[nx][ny] == -1
                is_water[nx][ny] = is_water[x][y] + 1
                q << [nx, ny]
            end
        end
    end
    is_water
end

```
```rust []
use std::collections::VecDeque;

impl Solution {
    pub fn highest_peak(is_water: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
        let mut is_water = is_water;
        let (n, m) = (is_water.len(), is_water[0].len());
        let mut q = VecDeque::new();
        
        for i in 0..n {
            for j in 0..m {
                if is_water[i][j] == 1 {
                    q.push_back((i, j));
                    is_water[i][j] = 0;
                } else {
                    is_water[i][j] = -1;
                }
            }
        }
        
        let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)];
        while let Some((x, y)) = q.pop_front() {
            for &(dx, dy) in &directions {
                let (nx, ny) = (x as isize + dx, y as isize + dy);
                if nx >= 0 && ny >= 0 && nx < n as isize && ny < m as isize {
                    let (nx, ny) = (nx as usize, ny as usize);
                    if is_water[nx][ny] == -1 {
                        is_water[nx][ny] = is_water[x][y] + 1;
                        q.push_back((nx, ny));
                    }
                }
            }
        }
        is_water
    }
}

```
```dart []
import 'dart:collection';

class Solution {
  List<List<int>> highestPeak(List<List<int>> isWater) {
    int n = isWater.length, m = isWater[0].length;
    Queue<List<int>> q = Queue();
    
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (isWater[i][j] == 1) {
          q.add([i, j]);
          isWater[i][j] = 0;
        } else {
          isWater[i][j] = -1;
        }
      }
    }
    
    List<List<int>> directions = [[0, 1], [1, 0], [0, -1], [-1, 0]];
    while (q.isNotEmpty) {
      List<int> current = q.removeFirst();
      int x = current[0], y = current[1];
      
      for (List<int> dir in directions) {
        int nx = x + dir[0], ny = y + dir[1];
        if (nx >= 0 && ny >= 0 && nx < n && ny < m && isWater[nx][ny] == -1) {
          isWater[nx][ny] = isWater[x][y] + 1;
          q.add([nx, ny]);
        }
      }
    }
    return isWater;
  }
}

```
```swift []
import Foundation

class Solution {
    func highestPeak(_ isWater: [[Int]]) -> [[Int]] {
        var isWater = isWater
        let n = isWater.count, m = isWater[0].count
        var q = [(Int, Int)]()
        
        for i in 0..<n {
            for j in 0..<m {
                if isWater[i][j] == 1 {
                    q.append((i, j))
                    isWater[i][j] = 0
                } else {
                    isWater[i][j] = -1
                }
            }
        }
        
        let directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        while !q.isEmpty {
            let (x, y) = q.removeFirst()
            for (dx, dy) in directions {
                let nx = x + dx, ny = y + dy
                if nx >= 0 && ny >= 0 && nx < n && ny < m && isWater[nx][ny] == -1 {
                    isWater[nx][ny] = isWater[x][y] + 1
                    q.append((nx, ny))
                }
            }
        }
        return isWater
    }
}

```
```go []
package main

func highestPeak(isWater [][]int) [][]int {
    n, m := len(isWater), len(isWater[0])
    q := [][]int{}
    
    for i := 0; i < n; i++ {
        for j := 0; j < m; j++ {
            if isWater[i][j] == 1 {
                q = append(q, []int{i, j})
                isWater[i][j] = 0
            } else {
                isWater[i][j] = -1
            }
        }
    }
    
    directions := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
    for len(q) > 0 {
        x, y := q[0][0], q[0][1]
        q = q[1:]
        
        for _, dir := range directions {
            nx, ny := x + dir[0], y + dir[1]
            if nx >= 0 && ny >= 0 && nx < n && ny < m && isWater[nx][ny] == -1 {
                isWater[nx][ny] = isWater[x][y] + 1
                q = append(q, []int{nx, ny})
            }
        }
    }
    return isWater
}

``` 

['Breadth-First Search', 'Swift', 'Python', 'C++', 'Java', 'Go', 'Rust', 'Ruby', 'JavaScript', 'Dart']