Hostel visit Heap

In the solution why are they including this condition. if(pq.size()==k) {
if(dist(x, y)<pq.top()) {
pq.pop();
pq.push(dist(x, y));
}

@dips123deepali_c25f140838182212,
take the eq:
1 10 10
1 9 9
1 7 7
your pq will look
200
162
49
your ans now is 200
and now pq.size() == 3
query comes 1 5 5 now 25 < 200 so u need to pop first and then insert 25 so your pq will look
162
49
25
now if type 2 query comes then you can directly output 162 which is on top, if u haven’t removed 200 from top it would be on top

Got it .Now I completely understand.

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.