Skip to content

Commit 4888acc

Browse files
authored
Update 407-trapping-rain-water-ii.js
1 parent acc590e commit 4888acc

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

407-trapping-rain-water-ii.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,113 @@
1+
/**
2+
* @param {number[][]} heightMap
3+
* @return {number}
4+
*/
5+
6+
const trapRainWater = function (heightMap) {
7+
const pq = new PriorityQueue((a, b) => a[2] < b[2])
8+
const m = heightMap.length, n = heightMap[0].length
9+
10+
const visited = Array.from({ length: m }, () => Array(n).fill(false))
11+
12+
for(let i = 0; i < m; i++) {
13+
visited[i][0] = visited[i][n - 1] = true
14+
pq.push([i, 0, heightMap[i][0]])
15+
pq.push([i, n - 1, heightMap[i][n - 1]])
16+
}
17+
for(let j = 1; j < n - 1; j++) {
18+
visited[0][j] = visited[m - 1][j] = true
19+
pq.push([0, j, heightMap[0][j]], [m - 1, j, heightMap[m - 1][j]])
20+
}
21+
22+
let res = 0
23+
const dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]]
24+
while(!pq.isEmpty()) {
25+
const cur = pq.pop()
26+
const [r, c, h] = cur
27+
for(let dir of dirs) {
28+
const newR = r + dir[0], newC = c + dir[1]
29+
if(newR < 0 || newR >= m || newC < 0 || newC >= n || visited[newR][newC]) continue
30+
visited[newR][newC] = true
31+
res += Math.max(0, h - heightMap[newR][newC])
32+
pq.push([newR, newC, Math.max(h, heightMap[newR][newC])])
33+
}
34+
}
35+
36+
return res
37+
38+
}
39+
class PriorityQueue {
40+
constructor(comparator = (a, b) => a > b) {
41+
this.heap = []
42+
this.top = 0
43+
this.comparator = comparator
44+
}
45+
size() {
46+
return this.heap.length
47+
}
48+
isEmpty() {
49+
return this.size() === 0
50+
}
51+
peek() {
52+
return this.heap[this.top]
53+
}
54+
push(...values) {
55+
values.forEach((value) => {
56+
this.heap.push(value)
57+
this.siftUp()
58+
})
59+
return this.size()
60+
}
61+
pop() {
62+
const poppedValue = this.peek()
63+
const bottom = this.size() - 1
64+
if (bottom > this.top) {
65+
this.swap(this.top, bottom)
66+
}
67+
this.heap.pop()
68+
this.siftDown()
69+
return poppedValue
70+
}
71+
replace(value) {
72+
const replacedValue = this.peek()
73+
this.heap[this.top] = value
74+
this.siftDown()
75+
return replacedValue
76+
}
77+
78+
parent = (i) => ((i + 1) >>> 1) - 1
79+
left = (i) => (i << 1) + 1
80+
right = (i) => (i + 1) << 1
81+
greater = (i, j) => this.comparator(this.heap[i], this.heap[j])
82+
swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]])
83+
siftUp = () => {
84+
let node = this.size() - 1
85+
while (node > this.top && this.greater(node, this.parent(node))) {
86+
this.swap(node, this.parent(node))
87+
node = this.parent(node)
88+
}
89+
}
90+
siftDown = () => {
91+
let node = this.top
92+
while (
93+
(this.left(node) < this.size() && this.greater(this.left(node), node)) ||
94+
(this.right(node) < this.size() && this.greater(this.right(node), node))
95+
) {
96+
let maxChild =
97+
this.right(node) < this.size() &&
98+
this.greater(this.right(node), this.left(node))
99+
? this.right(node)
100+
: this.left(node)
101+
this.swap(node, maxChild)
102+
node = maxChild
103+
}
104+
}
105+
}
106+
107+
108+
109+
// another
110+
1111
/**
2112
* @param {number[][]} heightMap
3113
* @return {number}

0 commit comments

Comments
 (0)