1class MaxHeap {
2 insert(val) {
3 this.heap.push(val);
4 this.siftUp(this.heap.length - 1);
5 }
6 siftUp(i) {
7 while (i > 0) {
8 const parent = Math.floor((i - 1) / 2);
9 if (this.heap[i] <= this.heap[parent]) break;
10 [this.heap[i], this.heap[parent]] = [this.heap[parent], this.heap[i]];
11 i = parent;
12 }
13 }
14 extractMax() {
15 const max = this.heap[0];
16 this.heap[0] = this.heap.pop();
17 this.siftDown(0);
18 return max;
19 }
20}