|
| 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 | +/** |
| 70 | + * @param {number[][]} grid |
| 71 | + * @param {number[]} queries |
| 72 | + * @return {number[]} |
| 73 | + */ |
| 74 | +const maxPoints = function (grid, queries) { |
| 75 | + const m = grid.length, n = grid[0].length, k = queries.length |
| 76 | + const q = [...queries] |
| 77 | + const pq = new PQ((a, b) => a[1] < b[1]) |
| 78 | + const dirs = [[0, 1],[0, -1],[1, 0],[-1,0]] |
| 79 | + |
| 80 | + q.sort((a, b) => a - b) |
| 81 | + const hash = {} |
| 82 | + const visited = Array.from({ length: m }, () => Array(n).fill(false)) |
| 83 | + |
| 84 | + pq.push([[0, 0], grid[0][0]]) |
| 85 | + visited[0][0] = true |
| 86 | + |
| 87 | + let cnt = 0 |
| 88 | + for(const e of q) { |
| 89 | + while(!pq.isEmpty()) { |
| 90 | + const [coord, v] = pq.peek() |
| 91 | + const [r, c] = coord |
| 92 | + if(e <= v) break |
| 93 | + pq.pop() |
| 94 | + for(const [dr, dc] of dirs) { |
| 95 | + const nr = r + dr, nc = c + dc |
| 96 | + if(nr >= 0 && nr < m && nc >= 0 && nc < n && visited[nr][nc] === false) { |
| 97 | + visited[nr][nc] = true |
| 98 | + pq.push([[nr, nc], grid[nr][nc]]) |
| 99 | + } |
| 100 | + } |
| 101 | + cnt++ |
| 102 | + } |
| 103 | + |
| 104 | + hash[e] = cnt |
| 105 | + } |
| 106 | +// console.log(hash) |
| 107 | + return queries.map(e => hash[e]) |
| 108 | +} |
| 109 | + |
| 110 | +// another |
| 111 | + |
| 112 | + |
1 | 113 | /** |
2 | 114 | * @param {number[][]} grid |
3 | 115 | * @param {number[]} queries |
|
0 commit comments