Find K nearest Points to Point P in 2-dimensional plane

后端 未结 9 1726
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 04:27

Source: AMAZON INTERVIEW QUESTION

Given a point P and other N points in two dimensional space, find K points

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 04:36

    For just a single query...

    Maintain a heap of size k.

    For each point, calculate the distance to the point P. Insert that distance into the heap and delete the maximum from the heap if the size of the heap is greater than k.

    Running time: O(n log k)

提交回复
热议问题