🔥No BFS Simple Loop Detailed Explanation
🌟 IntuitionThe goal is to assign heights to all cells in the grid such that:
Cells with water (isWater[i][j] == 1) have a height of 0.
Adjacent cells differ in
# 🌟 Intuition  
The goal is to assign heights to all cells in the grid such that:  
1. Cells with water (`isWater[i][j] == 1`) have a height of `0`.  
2. Adjacent cells differ in height by at most `1`.  
3. The overall height values are maximized while satisfying the constraints.  

This can be achieved by propagating heights across the grid in **two passes**, ensuring that every cell considers its neighbors both from the previous and subsequent directions.  

**Important Note**:  
The order of these passes is flexible. You could choose:  
1. **Top-to-bottom and left-to-right** followed by **bottom-to-top and right-to-left** (used in the explanation below).  
2. Or reverse the order: **Bottom-to-top and right-to-left** followed by **top-to-bottom and left-to-right**.  

Both approaches yield the same result because we update the heights based on all neighbors in both directions.  

---

# 📚 Approach  

### Step 1: Initialization  
- Create a `height` matrix of size `R x C` (same as the grid `isWater`).  
- Initialize each cell in `height` to a very large value, such as `R + C` (ensures propagation works correctly).  
- Set water cells (`isWater[i][j] == 1`) to `0`, as their height is fixed.  

### Step 2: Propagation in Two Passes  
#### First Pass: Top-to-Bottom and Left-to-Right  
- Process the grid row by row from top to bottom and column by column from left to right.  
- For each cell `(i, j)`:
  - If the cell has a top neighbor (`i > 0`), update its height:  
    `height[i][j] = min(height[i][j], height[i - 1][j] + 1)`  
  - If the cell has a left neighbor (`j > 0`), update its height:  
    `height[i][j] = min(height[i][j], height[i][j - 1] + 1)`  

#### Second Pass: Bottom-to-Top and Right-to-Left  
- Process the grid row by row from bottom to top and column by column from right to left.  
- For each cell `(i, j)`:
  - If the cell has a bottom neighbor (`i < R - 1`), update its height:  
    `height[i][j] = min(height[i][j], height[i + 1][j] + 1)`  
  - If the cell has a right neighbor (`j < C - 1`), update its height:  
    `height[i][j] = min(height[i][j], height[i][j + 1] + 1)`  

---

# 📝 Why the Order of Passes Doesn’t Matter  
- The goal of the two passes is to ensure that **all neighbors** (top, bottom, left, and right) influence the height of each cell.  
- By updating the matrix twice, every cell eventually considers all possible directions.  
- Whether you start with top-to-bottom/left-to-right or bottom-to-top/right-to-left doesn’t affect the final result.  

---

# ⏳ Complexity  

- **Time Complexity**:  
  - \(O(R*C)\): Each cell is visited twice—once in each pass.  

- **Space Complexity**:  
  - \(O(R*C)\): For storing the `height` matrix.  

# Solution For Count Servers That Comunicate [Leetcode](https://leetcode.com/problems/count-servers-that-communicate/solutions/6317335/simple-servers-count-detailed-explanation/)

![Screenshot 2025-01-22 064747.png](https://assets.leetcode.com/users/images/a80e47f1-d5c7-4e99-b04e-22a5901c6cd5_1737508743.106095.png)



# 💻 Code  
```cpp []
class Solution {
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        int R = isWater.size();
        int C = isWater[0].size();
        vector<vector<int>> height(R, vector<int>(C, R + C)); // Initialize with a safe sentinel value

        // First pass: set water cells to 0 and propagate height from top-left to bottom-right
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                if (isWater[i][j] == 1) {
                    height[i][j] = 0; // Water cell has height 0
                } else {
                    if (i > 0) 
                        height[i][j] = min(height[i][j], height[i - 1][j] + 1); // Check from top
                    if (j > 0) 
                        height[i][j] = min(height[i][j], height[i][j - 1] + 1); // Check from left
                }
            }
        }

        // Second pass: propagate height from bottom-right to top-left
        for (int i = R - 1; i >= 0; i--) {
            for (int j = C - 1; j >= 0; j--) {
                if (i < R - 1) 
                    height[i][j] = min(height[i][j], height[i + 1][j] + 1); // Check from bottom
                if (j < C - 1) 
                    height[i][j] = min(height[i][j], height[i][j + 1] + 1); // Check from right
            }
        }

        return height;
    }
};
```
```Python []
class Solution:
    def highestPeak(self, isWater: List[List[int]]) -> List[List[int]]:
        R = len(isWater)
        C = len(isWater[0])
        height = [[float('inf')] * C for _ in range(R)]

        for i in range(R):
            for j in range(C):
                if isWater[i][j] == 1:
                    height[i][j] = 0
                else:
                    if i > 0:
                        height[i][j] = min(height[i][j], height[i - 1][j] + 1)
                    if j > 0:
                        height[i][j] = min(height[i][j], height[i][j - 1] + 1)

        for i in range(R - 1, -1, -1):
            for j in range(C - 1, -1, -1):
                if i < R - 1:
                    height[i][j] = min(height[i][j], height[i + 1][j] + 1)
                if j < C - 1:
                    height[i][j] = min(height[i][j], height[i][j + 1] + 1)

        return height
```
```javaScript []
var highestPeak = function(isWater) {
    const R = isWater.length;
    const C = isWater[0].length;
    const height = Array.from({ length: R }, () => Array(C).fill(Number.MAX_VALUE));

    for (let i = 0; i < R; i++) {
        for (let j = 0; j < C; j++) {
            if (isWater[i][j] === 1) {
                height[i][j] = 0;
            } else {
                if (i > 0) height[i][j] = Math.min(height[i][j], height[i - 1][j] + 1);
                if (j > 0) height[i][j] = Math.min(height[i][j], height[i][j - 1] + 1);
            }
        }
    }

    for (let i = R - 1; i >= 0; i--) {
        for (let j = C - 1; j >= 0; j--) {
            if (i < R - 1) height[i][j] = Math.min(height[i][j], height[i + 1][j] + 1);
            if (j < C - 1) height[i][j] = Math.min(height[i][j], height[i][j + 1] + 1);
        }
    }

    return height;
};
```
```Go []
func highestPeak(isWater [][]int) [][]int {
    R := len(isWater)
    C := len(isWater[0])
    height := make([][]int, R)

    for i := range height {
        height[i] = make([]int, C)
        for j := range height[i] {
            height[i][j] = 1<<31 - 1
        }
    }

    for i := 0; i < R; i++ {
        for j := 0; j < C; j++ {
            if isWater[i][j] == 1 {
                height[i][j] = 0
            } else {
                if i > 0 {
                    height[i][j] = min(height[i][j], height[i-1][j] + 1)
                }
                if j > 0 {
                    height[i][j] = min(height[i][j], height[i][j-1] + 1)
                }
            }
        }
    }

    for i := R - 1; i >= 0; i-- {
        for j := C - 1; j >= 0; j-- {
            if i < R - 1 {
                height[i][j] = min(height[i][j], height[i+1][j] + 1)
            }
            if j < C - 1 {
                height[i][j] = min(height[i][j], height[i][j+1] + 1)
            }
        }
    }

    return height
}

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}
```
['Array', 'Matrix', 'C++', 'Go', 'Python3', 'JavaScript']