Beginner Friendly Soln. || Step Wise Explanation || Clean Code
IntuitionThe problem asks to assign heights to cells in a grid, where water cells have height 0 and land cells have positive heights. The height of each land ce
# Intuition
The problem asks to assign heights to cells in a grid, where water cells have height 0 and land cells have positive heights. The height of each land cell should be the minimum distance to the nearest water cell.So this kinda look like BFS, infact this is a multisource BFS starting from all water cells.

# Approach
1. Initialize a matrix **ans** with the same dimensions as the input, setting water cells to 0 and land cells to 1e9 (indicating that heights are not initialized for these cells).
1. Create a queue **q** and add all water cell coordinates to it.
Queue **q** will store *{h , {i , j }}*,where h represents height, that will be 0 for all water cells and i , j represents positions of cells.
1. Perform BFS:
    - For each cell in the queue, check its four adjacent cells.
    - If an adjacent cell is within the region and its height is 1e9 that means currently unvisited,set its height to h + 1 i.e *ans[i][j] = h + 1*,where h is height of current cell.
    - Add the newly visited cell to the queue.
1. Continue until the queue is empty, which means all cells have been visited and they have some height now.

# Complexity
- Time complexity:
During BFS,we will traverse each cell once only,and its 4 neighbours.
**T.C - O(4 * n * m) ~ O(n * m)**

- Space complexity:
To store heights, we need to create answer array.
**S.C - O(n * m)**

# Code
```cpp []
class Solution {
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& arr) {
        int n = arr.size(),m = arr[0].size();
        vector<vector<int>> ans(n,vector<int>(m,1e9));
        queue<pair<int,pair<int,int>>> q;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                if(arr[i][j] == 1){
                    //water 
                    q.push({0,{i,j}});
                    ans[i][j] = 0;
                }
            }
        }
        int di[] = {0,-1,0,1};
        int dj[] = {-1,0,1,0};
        while(!q.empty()){
            auto it = q.front(); q.pop();
            int h = it.first,i = it.second.first,j = it.second.second;
            for(int k = 0;k < 4;k++){
                int ni = i + di[k],nj = j + dj[k];
                if(ni >= 0 && ni < n && nj >= 0 && nj < m && ans[ni][nj] == 1e9){
                    ans[ni][nj] = h + 1;
                    q.push({h+1,{ni,nj}});
                }
            }
        }
        return ans;
    }
};
```
['Array', 'Breadth-First Search', 'Matrix', 'C++']