/* * @lc app=leetcode id=407 lang=java * * [407] Trapping Rain Water II */ // @lc code=start class Solution { public int trapRainWater(int[][] heightMap) { PriorityQueue pq = new PriorityQueue<>((a, b)->{ return a.h - b.h; }); boolean[][] visited = visited(heightMap); pushBorders(heightMap, visited, pq); int[][] dirs = new int[][] { new int[] {1, 0}, new int[] {-1, 0}, new int[] {0, 1}, new int[] {0,-1} }; int cap = 0; while(!pq.isEmpty()) { Node cur = pq.poll(); int x, y; for(int i=0; i= 0 && x < heightMap.length && y >= 0 && y < heightMap[0].length && !visited[x][y]) { visited[x][y] = true; int curh = heightMap[x][y]; if(heightMap[x][y] < cur.h) { cap += cur.h - heightMap[x][y]; curh = cur.h; } pq.add(new Node(x, y, curh)); } } } return cap; } private void pushBorders(int[][] hm, boolean[][] visited, PriorityQueue pq) { int r = hm.length, c = hm[0].length; for(int i=0;i