Last active
May 5, 2020 10:53
-
-
Save rakibulalam/1ce5945088648803f499ddc851233e3a to your computer and use it in GitHub Desktop.
Descending based Priority Queue with less complexity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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