Skip to content

Commit 6d7d01a

Browse files
committed
0407 Solved
1 parent 9400bb3 commit 6d7d01a

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

Diff for: 0407-Trapping-Rain-Water-II/Animation/407.gif

10.2 MB
Loading

Diff for: 0407-Trapping-Rain-Water-II/Animation/407.m4v

17.7 MB
Binary file not shown.

Diff for: 0407-Trapping-Rain-Water-II/Animation/example.png

231 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# LeetCode 第 407 号问题:接雨水 II
2+
3+
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
4+
>
5+
> 同步博客:https://www.algomooc.com
6+
7+
题目来源于 LeetCode 上第 407 号问题:接雨水 II。题目难度为 Hard,目前通过率为 38% 。
8+
9+
### 题目描述
10+
11+
给你一个 m x n 的矩阵,其中的值均为正整数,代表二维高度图每个单元的高度,请计算图中形状最多能接多少体积的雨水。
12+
13+
**示例:**
14+
15+
```
16+
给出如下 3x6 的高度图:
17+
[
18+
[1,4,3,1,3,2],
19+
[3,2,1,3,2,4],
20+
[2,3,3,2,3,1]
21+
]
22+
23+
返回 4 。
24+
```
25+
26+
![](../Animation/example.png)
27+
28+
### 题目解析
29+
30+
在 1 个 2 维的矩阵中,每个格子都有其高度,问这个 2 维矩阵能够盛多少的水。首先我们分析,格子能够盛水的必要条件是其周围存在格子比当前格子高,这样水才能够被框得住,但是仔细一想,最外围的格子怎么办?它们是存不了水的,可以把最外围的格子想象成围栏,它们的作用就是保证里面格子的水不会流出来,所以我们就得先考虑这些格子,它们的高度直接决定了内部格子的蓄水量,但是这些格子也有局部性,一个格子的长短并不会影响矩阵当中所有的格子,但是它会影响与其相邻的格子,那么我们就需要有一个考虑的顺序,那就是优先考虑最外层最短的格子,由于每个格子都会影响到其周围的格子,内部格子也需要列入考虑范围,每次我们都考虑最短的格子,然后看其周围有没有没考虑过的比它还短的格子,于是就有了考虑的先后顺序:
31+
32+
1. 考虑最外层格子
33+
2. 选出最外层最短的格子
34+
3. 考虑该格子与其相邻的内部格子是否能盛水,并把这个内部格子也纳入考虑范围
35+
4. 在考虑范围内的所有格子中选出最短的格子,重复步骤 3
36+
37+
这里需要注意的是,每次纳入考虑范围的格子是加了水之后的高度,而不是之前的高度,原因想一下应该不难理解。另外就是可以使用了 “堆” 这个数据结构来帮助实现寻找 “当前考虑范围内最短的格子” 这个操作步骤。
38+
39+
### 动画描述
40+
41+
![](../Animation/407.gif)
42+
43+
### 代码实现
44+
45+
```java
46+
private class Pair {
47+
int x, y, h;
48+
Pair(int x, int y, int h) {
49+
this.x = x;
50+
this.y = y;
51+
this.h = h;
52+
}
53+
}
54+
55+
private int[] dirX = {0, 0, -1, 1};
56+
private int[] dirY = {-1, 1, 0, 0};
57+
58+
public int trapRainWater(int[][] heightMap) {
59+
if (heightMap.length == 0 || heightMap[0].length == 0) {
60+
return 0;
61+
}
62+
63+
int m = heightMap.length;
64+
int n = heightMap[0].length;
65+
66+
PriorityQueue<Pair> pq = new PriorityQueue<>(new Comparator<Pair>() {
67+
@Override
68+
public int compare(Pair a, Pair b) {
69+
return a.h - b.h;
70+
}
71+
});
72+
73+
boolean[][] visited = new boolean[m][n];
74+
75+
// 优先将外围的元素加入队列中
76+
for (int i = 0; i < n; ++i) {
77+
pq.offer(new Pair(0, i, heightMap[0][i]));
78+
pq.offer(new Pair(m - 1, i, heightMap[m - 1][i]));
79+
80+
visited[0][i] = true;
81+
visited[m - 1][i] = true;
82+
}
83+
84+
for (int i = 1; i < m - 1; ++i) {
85+
pq.offer(new Pair(i, 0, heightMap[i][0]));
86+
pq.offer(new Pair(i, n - 1, heightMap[i][n - 1]));
87+
88+
visited[i][0] = true;
89+
visited[i][n - 1] = true;
90+
}
91+
92+
int result = 0;
93+
while (!pq.isEmpty()) {
94+
Pair cur = pq.poll();
95+
96+
// 遍历当前位置上下左右四个方向
97+
for (int k = 0; k < 4; ++k) {
98+
int curX = cur.x + dirX[k];
99+
int curY = cur.y + dirY[k];
100+
101+
if (curX < 0 || curY < 0 || curX >= m || curY >= n || visited[curX][curY]) {
102+
continue;
103+
}
104+
105+
if (heightMap[curX][curY] < cur.h) {
106+
result += cur.h - heightMap[curX][curY];
107+
}
108+
109+
pq.offer(new Pair(curX, curY,
110+
Math.max(heightMap[curX][curY], cur.h)));
111+
visited[curX][curY] = true;
112+
}
113+
}
114+
115+
return result;
116+
}
117+
```
118+
119+
<br>
120+
121+
### 复杂度分析
122+
123+
因为使用了优先队列这个数据结构,每次元素出入队列的时间复杂度是 O(logn),于是我们可以得出整体时间复杂度是 `O(m*n*logm*n)`,当然,需要说明的是,这是最差时间复杂度,由于并不是所有的元素都一次性加入队列,平均时间复杂度要比这个来的低,具体是什么就得看输入数据了。空间复杂度是 `O(m*n)`,这里也不难理解。通过这道题,堆的用法又被很好地展现了出来。
124+
125+
126+
127+
![](../../Pictures/qrcode.jpg)

0 commit comments

Comments
 (0)