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