✅Easy C++ Solution| Intuition & Approach| Clean Code| Why BFS?
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is a classical bfs problem. We can start from the water cells which will make it a
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a classical bfs problem. We can start from the water cells which will make it a multisource bfs. \n\nWhy bfs? Since we are comparing the height with respect to the water which is at height zero, so height of land will be calculated with respect to nearest water cell. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirsty, we will start by storing all the water cells in a queue by storing their indexes. \n\nThen on the queue, we will run our bfs.\n\nSee the code for further implementation. Code is intuitive.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n\n# Code\n```\nclass Solution {\n    //this array is created to generate neighbour indexes\n    int dx[4] = {-1,0,1,0};\n    int dy[4] = {0,-1,0,1};\n\n    //check if the indes is valid or not, also check if it not a water cell and not visited height will zero at that index\n    bool isSafe(vector<vector<int>>& isWater, int r, int c, vector<vector<int>>& heights){\n        if(r < 0 || c < 0 || r == isWater.size() || c == isWater[0].size() || isWater[r][c] == 1 || heights[r][c] > 0){\n            return false;\n        }\n        return true;\n    }\n\n    void bfs(vector<vector<int>>& isWater, vector<vector<int>>& heights, queue<pair<int,int>>& q){\n        while(q.size() != 0){\n            int size = q.size();\n            while(size--){\n                int i = q.front().first;\n                int j = q.front().second;\n                q.pop();\n\n                int height = heights[i][j];\n\n                //generating neighbour indexes\n                for(int b = 0; b < 4; b++){\n                    int nI = i + dx[b];\n                    int nJ = j + dy[b];\n\n                    if(isSafe(isWater,nI,nJ,heights)){\n                        heights[nI][nJ] = height + 1;\n                        q.push({nI,nJ});\n                    }\n                }\n            }\n        }\n    }\npublic:\n    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {\n        int m = isWater.size();\n        int n = isWater[0].size();\n\n        vector<vector<int>> heights(m,vector<int>(n,0));\n        queue<pair<int,int>> q;\n\n        for(int i = 0; i < m; i++){\n            for(int j = 0; j < n; j++){\n                if(isWater[i][j] == 1){\n                    q.push({i,j});\n                }\n            }\n        }\n\n        bfs(isWater,heights,q);\n\n        return heights;\n    }\n};\n```
['Breadth-First Search', 'C++']