407. Trapping Rain Water II
题目链接:
https://leetcode.com/problems...
参考discussion里的解法:
https://discuss.leetcode.com/...
参考博客里的解释:
http://www.cnblogs.com/grandy...
public class Solution {
public int trapRainWater(int[][] heightMap) {
// base case
if(heightMap.length == 0 || heightMap[0].length == 0) return 0;
int m = heightMap.length, n = heightMap[0].length;
// bfs, heap
boolean[][] visited = new boolean[m][n];
// add 4 sides first, since 4 side can not store water
PriorityQueue<Cell> minHeap = new PriorityQueue<>((m+n), (a, b) -> a.h - b.h);
// 1st col and last col
for(int i = 0; i < m; i++) {
minHeap.offer(new Cell(i, 0, heightMap[i][0]));
visited[i][0] = true;
minHeap.offer(new Cell(i, n - 1, heightMap[i][n-1]));
visited[i][n-1] = true;
}
// 1st row and last row
for(int j = 0; j < n; j++) {
minHeap.offer(new Cell(0, j, heightMap[0][j]));
visited[0][j] = true;
minHeap.offer(new Cell(m-1, j, heightMap[m-1][j]));
visited[m-1][j] = true;
}
// bfs find water
int res = 0;
while(!minHeap.isEmpty()) {
Cell cur = minHeap.poll();
for(int[] dir : dirs) {
int nx = cur.x + dir[0], ny = cur.y + dir[1];
if(nx >= 0 && nx < m && ny >= 0 && ny < n && !visited[nx][ny]) {
visited[nx][ny] = true;
if(heightMap[nx][ny] < cur.h) res += cur.h - heightMap[nx][ny];
minHeap.offer(new Cell(nx, ny, Math.max(cur.h, heightMap[nx][ny])));
}
}
}
return res;
}
int[][] dirs = new int[][] {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
}
class Cell {
int x;
int y;
int h;
Cell(int x, int y, int h) {
this.x = x;
this.y = y;
this.h = h;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。