Please help me its not accepting the all tc
hi @kailash_01
these two operations are expensive
due to this time complexity will goes to O(n^2) in worst Case
while(t--){
pq1.push(pq.top());
pq.pop();
}
cout<<pq.top()<<endl;
while(!pq1.empty()){
pq.push(pq1.top());
pq1.pop();
}
to reduce this to O(n) try to use multiset
you can take help from this if needed
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course
please explain me in the refrence code line no18 and line no 22 i m not understanding the piece —>>> if (distances.size() > k) { distances.erase(–distances.end()); } } else { cout << *distances.rbegin() << ‘\n’; }
in line 18
if (distances.size() > k) {
distances.erase(–distances.end());
}
i am deleting non essential elements
as distance.end() is pointing to end of multiset not the last element
so first we decrease so that it point to last elment then we erase it
in line 20
we have to print kth element so instead of poping all k-1 elements and then again pushing to multiset
i just use reverse iterator which point to last element and for printing its value use *
for more details of these stl iterator you can refer Doucumentation