[Python/Java] BFS from cell with lowest height - Clean & Concise
Idea\n- Firstly, we fill water cells with height = 0 and start to fill from their cells.\n- We prioritize filling cells with lowest height by using BFS (using q
**Idea**\n- Firstly, we fill water cells with height = 0 and start to fill from their cells.\n- We prioritize filling cells with lowest height by using BFS (using queue, first in first out).\n- Because `any two adjacent cells must have an absolute height difference of at most 1`, we can set height of neighbors maximum 1 height bigger than current cell.\n\n![image](https://assets.leetcode.com/users/images/14c85cfb-1c24-4629-8aae-d1a216c899d6_1613836714.1379192.png)\n\n\n**Complexity:**\n- Time & Space: `O(m * n)`\n\n\n**Python**\n```python\nclass Solution(object):\n    def highestPeak(self, isWater):\n        m, n = len(isWater), len(isWater[0])\n        height = [[-1] * n for _ in range(m)]\n        bfs = deque([])\n        for r in range(m):\n            for c in range(n):\n                if isWater[r][c] == 1:\n                    bfs.append((r, c))\n                    height[r][c] = 0\n\n        DIR = [0, 1, 0, -1, 0]\n        while bfs:\n            r, c = bfs.popleft()\n            for i in range(4):\n                nr, nc = r + DIR[i], c + DIR[i + 1]\n                if nr < 0 or nr == m or nc < 0 or nc == n or height[nr][nc] != -1: continue\n                height[nr][nc] = height[r][c] + 1\n                bfs.append((nr, nc))\n        return height\n```\n\n**Java**\n```java\nclass Solution {\n    public int[][] highestPeak(int[][] isWater) {\n        int m = isWater.length, n = isWater[0].length;\n        Queue<int[]> bfs = new LinkedList<>();\n        int[][] height = new int[m][n];\n        for (int r = 0; r < m; r++) {\n            for (int c = 0; c < n; c++) {\n                if (isWater[r][c] == 1) {\n                    height[r][c] = 0;\n                    bfs.offer(new int[]{r, c});\n                } else {\n                    height[r][c] = -1;\n                }\n            }\n        }\n        int[] DIR = new int[]{0, 1, 0, -1, 0};\n        while (!bfs.isEmpty()) {\n            int[] top = bfs.poll();\n            int r = top[0], c = top[1];\n            for (int i = 0; i < 4; i++) {\n                int nr = r + DIR[i], nc = c + DIR[i+1];\n                if (nr < 0 || nr == m || nc < 0 || nc == n || height[nr][nc] != -1) continue;\n                height[nr][nc] = height[r][c] + 1;\n                bfs.offer(new int[]{nr, nc});\n            }\n        }\n        return height;\n    }\n}\n```
[]