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

后端 未结 9 1716
佛祖请我去吃肉
佛祖请我去吃肉 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:56

    What is wrong with below approach ?

    1) Calculate distance from given point to other points.

    2) Store the distance and index of that point to TreeMap map

    3) Select top K elements from map. It's values will give index of Point element from points array.

    The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time,

提交回复
热议问题