|
| 1 | +/** |
| 2 | + * @param {number[][]} orders |
| 3 | + * @return {number} |
| 4 | + */ |
| 5 | +const getNumberOfBacklogOrders = function (orders) { |
| 6 | + const h0 = new PriorityQueue((a, b) => a[0] > b[0]) |
| 7 | + const h1 = new PriorityQueue((a, b) => a[0] > b[0]) |
| 8 | + const P = 10 ** 9 + 7 |
| 9 | + const { min } = Math |
| 10 | + |
| 11 | + while (!h0.isEmpty()) h0.pop() |
| 12 | + while (!h1.isEmpty()) h1.pop() |
| 13 | + let i, |
| 14 | + j, |
| 15 | + i1, |
| 16 | + j1, |
| 17 | + ans = 0 |
| 18 | + for (let c of orders) { |
| 19 | + i = c[0] |
| 20 | + j = c[1] |
| 21 | + if (c[2]) { |
| 22 | + while (!h0.isEmpty() && h0.peek()[0] >= i) { |
| 23 | + i1 = h0.peek()[0] |
| 24 | + j1 = h0.peek()[1] |
| 25 | + h0.pop() |
| 26 | + if (j > j1) j -= j1 |
| 27 | + else { |
| 28 | + j1 -= j |
| 29 | + j = 0 |
| 30 | + if (j1) h0.push([i1, j1]) |
| 31 | + break |
| 32 | + } |
| 33 | + } |
| 34 | + if (j) h1.push([-i, j]) |
| 35 | + } else { |
| 36 | + while (!h1.isEmpty() && -h1.peek()[0] <= i) { |
| 37 | + i1 = h1.peek()[0] |
| 38 | + j1 = h1.peek()[1] |
| 39 | + h1.pop() |
| 40 | + if (j > j1) j -= j1 |
| 41 | + else { |
| 42 | + j1 -= j |
| 43 | + j = 0 |
| 44 | + if (j1) h1.push([i1, j1]) |
| 45 | + break |
| 46 | + } |
| 47 | + } |
| 48 | + if (j) h0.push([i, j]) |
| 49 | + } |
| 50 | + } |
| 51 | + while (!h0.isEmpty()) { |
| 52 | + ans += h0.peek()[1] |
| 53 | + h0.pop() |
| 54 | + if (ans >= P) ans -= P |
| 55 | + } |
| 56 | + while (!h1.isEmpty()) { |
| 57 | + ans += h1.peek()[1] |
| 58 | + h1.pop() |
| 59 | + if (ans >= P) ans -= P |
| 60 | + } |
| 61 | + return ans |
| 62 | +} |
| 63 | + |
| 64 | +class PriorityQueue { |
| 65 | + constructor(comparator = (a, b) => a > b) { |
| 66 | + this.heap = [] |
| 67 | + this.top = 0 |
| 68 | + this.comparator = comparator |
| 69 | + } |
| 70 | + size() { |
| 71 | + return this.heap.length |
| 72 | + } |
| 73 | + isEmpty() { |
| 74 | + return this.size() === 0 |
| 75 | + } |
| 76 | + peek() { |
| 77 | + return this.heap[this.top] |
| 78 | + } |
| 79 | + push(...values) { |
| 80 | + values.forEach((value) => { |
| 81 | + this.heap.push(value) |
| 82 | + this.siftUp() |
| 83 | + }) |
| 84 | + return this.size() |
| 85 | + } |
| 86 | + pop() { |
| 87 | + const poppedValue = this.peek() |
| 88 | + const bottom = this.size() - 1 |
| 89 | + if (bottom > this.top) { |
| 90 | + this.swap(this.top, bottom) |
| 91 | + } |
| 92 | + this.heap.pop() |
| 93 | + this.siftDown() |
| 94 | + return poppedValue |
| 95 | + } |
| 96 | + replace(value) { |
| 97 | + const replacedValue = this.peek() |
| 98 | + this.heap[this.top] = value |
| 99 | + this.siftDown() |
| 100 | + return replacedValue |
| 101 | + } |
| 102 | + |
| 103 | + parent = (i) => ((i + 1) >>> 1) - 1 |
| 104 | + left = (i) => (i << 1) + 1 |
| 105 | + right = (i) => (i + 1) << 1 |
| 106 | + greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) |
| 107 | + swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) |
| 108 | + siftUp = () => { |
| 109 | + let node = this.size() - 1 |
| 110 | + while (node > this.top && this.greater(node, this.parent(node))) { |
| 111 | + this.swap(node, this.parent(node)) |
| 112 | + node = this.parent(node) |
| 113 | + } |
| 114 | + } |
| 115 | + siftDown = () => { |
| 116 | + let node = this.top |
| 117 | + while ( |
| 118 | + (this.left(node) < this.size() && this.greater(this.left(node), node)) || |
| 119 | + (this.right(node) < this.size() && this.greater(this.right(node), node)) |
| 120 | + ) { |
| 121 | + let maxChild = |
| 122 | + this.right(node) < this.size() && |
| 123 | + this.greater(this.right(node), this.left(node)) |
| 124 | + ? this.right(node) |
| 125 | + : this.left(node) |
| 126 | + this.swap(node, maxChild) |
| 127 | + node = maxChild |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments