I am failing a test. I have used a set instead of a priority queue .
- Why am I failing a test case?
- Does the priority queue give any advantage in terms of time complexity?
I am failing a test. I have used a set instead of a priority queue .
Hi @ankurguria
try making the if else cases like these
if(z==2){
cout<<pq.top()<<endl;
}
else if(z==1){
long long int x,y;
cin>>x>>y;
long long int d=xx+yy;
if(pq.size()<k){
pq.push(d);
}
else if(pq.top()>d){
pq.pop();
pq.push(d);
}
}
here i m maining a pq of size k. if i want to push a dist, if size is already==k, i remove the farthest hostel hostel and insert the new dist.
its of advantage to use priority queue here since u can access the smallest element from the top without having to iterate till the end if we had used sets,etc.
moreover,it keep all the element sorted. Time complexity of insertion of a element in case of pq is logn(n=current length)
Hope dis helps.
If something is still unclear, feel free to post ur doubt here.
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.