-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path407. Trapping Rain Water II
55 lines (42 loc) · 1.62 KB
/
407. Trapping Rain Water II
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
typedef pair<int, pair<int, int>> PP;
vector<vector<int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int trapRainWater(vector<vector<int>>& heightMap) {
int m = heightMap.size();
int n = heightMap[0].size();
priority_queue<PP, vector<PP>, greater<>> boundaryCells;
vector<vector<bool>> visited(m, vector<bool>(n, false));
for (int r = 0; r < m; ++r) {
for (int c : {0, n - 1}) { //0 : left most boundary, cols-1 right most boundary
boundaryCells.push({heightMap[r][c], {r, c}});
visited[r][c] = true;
}
}
for (int c = 0; c < n; ++c) {
for (int r : {0, m - 1}) { //0 : top most boundary, rows-1 bottom most boundary
boundaryCells.push({heightMap[r][c], {r, c}});
visited[r][c] = true;
}
}
int water = 0;
while(!boundaryCells.empty()){
PP p = boundaryCells.top();
boundaryCells.pop();
int heigth = p.first;
int i = p.second.first;
int j = p.second.second;
//find neighbourss
for(vector<int>& dir: directions){
int i_ = i + dir[0];
int j_ = j + dir[1];
if(i_ >= 0 && i_ <m && j_ >= 0 && j_ <n && !visited[i_][j_]){
water += max(heigth - heightMap[i_][j_], 0);
boundaryCells.push({max(heigth, heightMap[i_][j_]), {i_, j_}});
visited[i_][j_] = true;
}
}
}
return water;
}
};