|  | 
|  | 1 | +/** | 
|  | 2 | + * @param {number[]} dist | 
|  | 3 | + * @param {number[]} speed | 
|  | 4 | + * @return {number} | 
|  | 5 | + */ | 
|  | 6 | +const eliminateMaximum = function(dist, speed) { | 
|  | 7 | +  const pq = new PriorityQueue((a, b) => a[0] / a[1] < b[0] / b[1]) | 
|  | 8 | +  const n = dist.length | 
|  | 9 | +  for(let i = 0; i < n; i++) { | 
|  | 10 | +    pq.push([dist[i], speed[i]]) | 
|  | 11 | +  } | 
|  | 12 | +  let res = 0 | 
|  | 13 | +  while(true) { | 
|  | 14 | +    if(pq.isEmpty()) break | 
|  | 15 | +    if(pq.peek()[0] < 0) break | 
|  | 16 | +    const tmp = pq.pop() | 
|  | 17 | +    if(tmp[0] <= res * tmp[1]) break | 
|  | 18 | +    res++ | 
|  | 19 | +  } | 
|  | 20 | +   | 
|  | 21 | +  return res | 
|  | 22 | +}; | 
|  | 23 | + | 
|  | 24 | +class PriorityQueue { | 
|  | 25 | +  constructor(comparator = (a, b) => a > b) { | 
|  | 26 | +    this.heap = [] | 
|  | 27 | +    this.top = 0 | 
|  | 28 | +    this.comparator = comparator | 
|  | 29 | +  } | 
|  | 30 | +  size() { | 
|  | 31 | +    return this.heap.length | 
|  | 32 | +  } | 
|  | 33 | +  isEmpty() { | 
|  | 34 | +    return this.size() === 0 | 
|  | 35 | +  } | 
|  | 36 | +  peek() { | 
|  | 37 | +    return this.heap[this.top] | 
|  | 38 | +  } | 
|  | 39 | +  push(...values) { | 
|  | 40 | +    values.forEach((value) => { | 
|  | 41 | +      this.heap.push(value) | 
|  | 42 | +      this.siftUp() | 
|  | 43 | +    }) | 
|  | 44 | +    return this.size() | 
|  | 45 | +  } | 
|  | 46 | +  pop() { | 
|  | 47 | +    const poppedValue = this.peek() | 
|  | 48 | +    const bottom = this.size() - 1 | 
|  | 49 | +    if (bottom > this.top) { | 
|  | 50 | +      this.swap(this.top, bottom) | 
|  | 51 | +    } | 
|  | 52 | +    this.heap.pop() | 
|  | 53 | +    this.siftDown() | 
|  | 54 | +    return poppedValue | 
|  | 55 | +  } | 
|  | 56 | +  replace(value) { | 
|  | 57 | +    const replacedValue = this.peek() | 
|  | 58 | +    this.heap[this.top] = value | 
|  | 59 | +    this.siftDown() | 
|  | 60 | +    return replacedValue | 
|  | 61 | +  } | 
|  | 62 | + | 
|  | 63 | +  parent = (i) => ((i + 1) >>> 1) - 1 | 
|  | 64 | +  left = (i) => (i << 1) + 1 | 
|  | 65 | +  right = (i) => (i + 1) << 1 | 
|  | 66 | +  greater = (i, j) => this.comparator(this.heap[i], this.heap[j]) | 
|  | 67 | +  swap = (i, j) => ([this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]) | 
|  | 68 | +  siftUp = () => { | 
|  | 69 | +    let node = this.size() - 1 | 
|  | 70 | +    while (node > this.top && this.greater(node, this.parent(node))) { | 
|  | 71 | +      this.swap(node, this.parent(node)) | 
|  | 72 | +      node = this.parent(node) | 
|  | 73 | +    } | 
|  | 74 | +  } | 
|  | 75 | +  siftDown = () => { | 
|  | 76 | +    let node = this.top | 
|  | 77 | +    while ( | 
|  | 78 | +      (this.left(node) < this.size() && this.greater(this.left(node), node)) || | 
|  | 79 | +      (this.right(node) < this.size() && this.greater(this.right(node), node)) | 
|  | 80 | +    ) { | 
|  | 81 | +      let maxChild = | 
|  | 82 | +        this.right(node) < this.size() && | 
|  | 83 | +        this.greater(this.right(node), this.left(node)) | 
|  | 84 | +          ? this.right(node) | 
|  | 85 | +          : this.left(node) | 
|  | 86 | +      this.swap(node, maxChild) | 
|  | 87 | +      node = maxChild | 
|  | 88 | +    } | 
|  | 89 | +  } | 
|  | 90 | +} | 
0 commit comments