Hostel Visit Segmentation Fault

My approach:
take two stack, if query==2 then,
untill count==k, pop it from one stack and push it in the another stack.
After print the top element when count==3(k), then again push elements in this stack from other stack.

it worked…my mistake, count=1

1 Like

@Kinjal you are getting segmentation fault cause inside while loop at line you have popped out all k items from stack and then trying to print s.top() from empty stack.

still getting wrong answer and TLE

that is cause you have to print kth nearest point, but your code is never checking which one is nearest just printing kth point.

What do you mean by that?

ohh, is it mean that I have to sort all the distance first in the stack

getting TLE

sorting stack every time will give you TLE for sure. look at this code and then try to make changes in your code.https://ide.codingblocks.com/s/278003

if(pq.size()==k) {
                if(rocketDistance(x, y)<pq.top()) {
                    pq.pop();
                    pq.push(rocketDistance(x, y));
                }
            }

This can be happen that still rocketDistance(x, y)<pq.top(), right!!

sorry didn’t get what you pointed out.

I understood the problem solution. Max Heap. Sorry.

1 Like

okay. mark this doubt as resolved if you don’t have any further queries.

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.