Hotel Visit - Required Explanation

Can you please help me with the approach that how can I solve this question using heap?

@koshimagoyal97
The problem says that we need to handle 2 type of queries.

  1. we just discovered new Hostel with Coordinates (x,y).
  2. We need to provide kth nearest hostel with the Coordinates discovered till now.
    What data structure you are going to use for storing the distances?
    Let me assume you may use ArrayList/Set and store only the mininum of the distances discovered till now,
    Now if you use this how would you be able to maintain the kth minimum distance always?
    Therefore here comes the Heap.It will keep the account of kth minimum always.
    Try to read about Priority Queue as it is inbuilt data structure in Java to support Heap implementation.
    Thankyou,I hope you got to know why heap is the only choice here,not other Data Structures.
1 Like

Thanks alot. It resolved my problem.