Skip to content

Instantly share code, notes, and snippets.

@rakibulalam
Last active May 5, 2020 10:53
Show Gist options
  • Save rakibulalam/1ce5945088648803f499ddc851233e3a to your computer and use it in GitHub Desktop.
Save rakibulalam/1ce5945088648803f499ddc851233e3a to your computer and use it in GitHub Desktop.
Descending based Priority Queue with less complexity
/**
* Descending based Priority Queue with less complexity
**/
class PriorityQueue {
constructor() {
this.queue = [];
}
enqueue(element) {
this.queue=[...this.queue, element].sort(([ v, p ], [ v1, p1 ]) => p1 - p);
// this.queue.push(element).sort(([ v, p ], [ v1, p1 ]) =>p1 - p);
}
dequeue() {
return this.queue.shift();
}
isEmpty() {
return this.queue.length === 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment