Hostel Visit : time limit error

I am using HEAP to solve this question.
I am facing time limit error.
Please look into the code.

try to add only k elements in the max heap
instead of adding all the elements
create max heap or you can use inbuild priority queue instead .

Why should I create MAX HEAP ? It’s given in the question that the DEAN will take " K nearest hostels ".

we use max heap because we can optimize space by adding only k elements in maxheap.
so we add k minimum elements in maxheap
so that at peek you will always have the kth nearest hostel

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.

I applied the condition in my code but still it’s not working. Please look into the code.

hi @mananaroramail
your code is correct you just need to make priority queue of Long type and calculate the distance in long as well because distance value can go beyond integer range as we are taking the square of the integer value.