Skip to content

Commit

Permalink
Create PriorityQueueV2.js
Browse files Browse the repository at this point in the history
  • Loading branch information
everthis authored Jul 11, 2024
1 parent df41089 commit 4bebfcf
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/PriorityQueueV2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @param {number[][]} buildings
* @return {number[][]}
*/

class PQ {
constructor(comparator) {
this.pq = [];
this.compare = comparator;
}

get size() {
return this.pq.length;
}

swap(i, j) {
const pq = this.pq;
[pq[i], pq[j]] = [pq[j], pq[i]];
}

siftUp(i) {
if (i === 0) return;
const pq = this.pq;
const j = Math.floor((i - 1) / 2);
if (this.better(i, j)) {
this.swap(i, j);
this.siftUp(j);
}
}
better(i, j) {
const pq = this.pq
return this.compare(pq[i], pq[j]) < 0
}
siftDown(i) {
const pq = this.pq;
const [l, r] = [i * 2 + 1, i * 2 + 2];
if (l >= this.size) return;
const j = r >= this.size || this.better(l, r) ? l : r;
if (this.compare(pq[j], pq[i]) < 0) {
this.swap(j, i);
this.siftDown(j);
}
}

insert(val) {
this.pq.push(val);
this.siftUp(this.size - 1);
}

peak() {
return this.pq[0];
}

pull() {
const val = this.pq[0];
this.swap(0, this.size - 1);
this.pq.pop();
this.siftDown(0);
return val;
}
}

0 comments on commit 4bebfcf

Please sign in to comment.