-
Notifications
You must be signed in to change notification settings - Fork 53
Description
Hello. Thank you very much for such a great implementation of the PriorityQueue.
I would like to suggest adding a new feature to this implementation. Let me provide an example to simplify the explanation.
I have an array of integers and I need to create MaxPriorityQueue based on this array. With the current API, I see only two ways to solve my task:
- Convert the integers array to a 2D array
[[<SOME_INT1>, <SOME_INT1>], [<SOME_INT2>, <SOME_INT2>]]and then pass this array toMaxPriorityQueue.frommethod. - The other approach is to use a constrictor with priority property
new MaxPriorityQueue({ priority: (num) => num });and then call theenqueuemethod in a loop.
Both approaches have some significant disadvantages. At least they require to write some extra loop to initialize a new MaxPriorityQueue. In addition, the first approach requires the creation of a new array which means extra memory usage and an extra pass along array (O(n) extra space and O(n) extra time). The second approach requires adding all the elements with enqueue call, the time complexity of which is O(log(n)). So, adding n elements will require O(n*log(n)) time instead of O(n) if we use MaxPriorityQueue.from call (I suppose you use heapification in the from implementation).
So, considering the disadvantages of the two approaches above, I believe we should have the possibility to use priority property in combinations with from (the name might be different), e.g.
const intArray = [1, 2, 3];
const pq = new MinPriorityQueue({
priority: myInt => myInt,
from: intArray,
});In this way, we would be able to init a new PriorityQueue without any extra loops and memory/time consumptions.
I would be glad to contribute and implement this feature.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status