Hostel Visit timelimit


Got timelimit in 1 out of 2 testcase

Heap approach is a good approach but you should tweak it a little bit .First add k numbers in an array and find a kth closest using a simple loop. Then for each query of type 1, if incoming point is closer than existing kth closest, then only compute the answer again else just add the point to the heap.

Not got your point. Please explain a little more.

If already computed and is A. Now next point that is input is closer than point A, that means kth smallest point will change. Now compute the kth smallest. Else if the input point is already farther than A, then it doesn’t change the kth closest so just add it to heap

What about this?
Did some minor editing in main()
1 testcase failed out of 2.

It was giving wrong answer because you used a heap of int. Changed to long and it worked but is still giving TLE. Think of improving the complexity either using Priority Queue or leveling the tree(you could refer to the GFG article, it has a good explanation).

Please give me gfg article link that you are talking about

Read the last method

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.