I have used a set approach, in which I will store only smallest K number, It worked perfectly in the sample test case however it gave the wrong answer when I submitted the code.
Code - https://ideone.com/SWYfuX
Doubt in the question of the Hostel Visit
And ONe thing more, previously I had used the
auto it = --ss.end(); //isn’t working please explain…
For this question, i would recommend you to use heap data structure. This is an easy question for practice heaps.
Simply make a min heap, keep adding distances.
Whenever you get a query, pop k-1 elements temporarily and give the answer!
For this implementation, you can have a look at this very simple and easy code here.
In your code, you are not maintaining all distances.
You need to store all distances, and then give the answer.
Please study the given sample test case to know whats the demand in the question.
Can you please give a test case where my code will fail.
10 3
1 10 10
1 9 9
1 -8 -8
1 2 2
2
1 7 7
2
1 6 6
1 5 5
2
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.