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