4
var maxEvents = function (events) {5
// sort events by their start time6
events.sort((a, b) => a[0] - b[0]);8
// create priority queue to sort events by end time, the events that have saem start time9
let minHeap = new PriorityQueue((a, b) => a - b);15
while (i < len || !minHeap.isEmpty()) {16
if (minHeap.isEmpty()) d = events[i][0]; // start with event from events list18
while (i < len && events[i][0] <= d)19
// Add all events that have start time <= day `d` into PQ20
minHeap.add(events[i++][1]); // add ending time to the minHeap, so when we pull we get event that is ending first22
minHeap.poll(); // make sure we have some event attent26
// we finished task of day 'd' Remove events that are already closed ie endTime < d as we cant attend it27
while (!minHeap.isEmpty() && minHeap.peek() < d) minHeap.poll();32
/*******************standard priority queue implementation used in all leetcode javascript solutions****************/36
* @param {function} compareFunction - compares child and parent element37
* to see if they should swap. If return value is less than 0 it will38
* swap to prioritize the child.40
constructor(compareFunction) {42
this.compareFunction = compareFunction;50
return this.store.length;53
return this.store.length === 0;57
if (this.size() < 2) {58
return this.store.pop();60
const result = this.store[0];61
this.store[0] = this.store.pop();68
this.heapifyUp(this.size() - 1);73
const parent = Math.floor((child - 1) / 2);75
if (this.shouldSwap(child, parent)) {76
[this.store[child], this.store[parent]] = [89
let [child, child2] = [1, 2]90
.map((x) => parent * 2 + x)91
.filter((x) => x < this.size());92
if (this.shouldSwap(child2, child)) {96
if (this.shouldSwap(child, parent)) {97
[this.store[child], this.store[parent]] = [108
shouldSwap(child, parent) {110
child && this.compareFunction(this.store[child], this.store[parent]) < 0