Hostel Visit (Heaps)


How i am getting TLE in 1st test case ?

hi @taritkandpal

this will definetly give tle because you are copying whole prioirty_queue
at each query (because it is passed by value so copied every time when function called)

hint : pass by value but then you have to insert remaning (k-1) element inside priority queue which may agian lead to TLE so best practice is to use multiset in this case
if you don’t understand hint Please try first then come back you will understand

i hope this help you

1 Like


Here i useed multiset but now i am getting TLE in the second test case.

the problem remains same that
for each query you are iterating k times
so time compexity is O(n*k)
this will give tle

so what should be done?

make multiset of size k only and when query comes
print last element of multiset this will done in O(1)
so resultant time complexity will be O(n)

reference code : if needed

1 Like