Can’t we simply use a priority queue here? That would take a single traversal O(n) (because it takes O(n) to create a priority queue) and make implementation a bit easier?
Using a priority queue
- doing that the complexity goes to nlogn since insertion in priority queue takes logn time and inserting n elements would mean nlogn
- how will u implement it using a priority queue each time u only get the top element which is either min or max which would mean it will be tough to keep track of multiple sequences together
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.