Find the nearest dot in a 2D space

前端 未结 4 1364
庸人自扰
庸人自扰 2021-02-19 21:17

Yesterday I read a problem which can be translated into the following problem with slight modification:

The coordinate of a dot is expressed by (x, y) in a 2D space.

4条回答
  •  别那么骄傲
    2021-02-19 22:06

    If the array is not sorted in any way, then it is not possible to do any faster than O(n), as you have to check every point to make sure it is not closer than anything you have found so far.

    You CAN do some preprocessing to sort the points or pack them into some structure, then do searches on that structure. In this case, while the preprocessing step may be slower than O(n), the individual searches may be faster. If you have many points to check against the same set of points, this can be advantageous.

提交回复
热议问题