Redian新闻
>
找最近的点,这题咋解?
avatar
找最近的点,这题咋解?# JobHunting - 待字闺中
h*g
1
You are given a list of points in the plane, write a program that
outputs each point along with the point that are closest
to it.
The order is less then O(n^2) .
For example, given a set of points where each line is of the form:
ID x-coordinate y-coordinate
1 0.0 0.0
2 10.1 -10.1
3 -12.2 12.2
4 38.3 38.3
5 79.99 179.99
Your program should output:
1 2
2 1
3 1
4 1
5 4
avatar
l*8
2
如果要求O(n^2), 那么最naive办法都可以了。
如果要快些的话,好像可以用Quad-tree.

【在 h*****g 的大作中提到】
: You are given a list of points in the plane, write a program that
: outputs each point along with the point that are closest
: to it.
: The order is less then O(n^2) .
: For example, given a set of points where each line is of the form:
: ID x-coordinate y-coordinate
: 1 0.0 0.0
: 2 10.1 -10.1
: 3 -12.2 12.2
: 4 38.3 38.3

avatar
h*g
3
我看到 一种 nlogn 的解法,但感觉不对。。
Approach:
First Have all the points in an array and sort them according to their X
axis, which would give:
3 -12.2 12.2
1 0.0 0.0
2 10.1 -10.1
4 38.3 38.3
5 79.99 179.99
Then for every point P, calculate the distance from 1 point in the front and
1 point in the back:
For Ex: Point with Id=3 (-12.2, 12.2) does not have 1 point
in the back.
Always store only min distance and the corresponding point Ids
Then Sort the points according to their Y Axis, which would give:
2 10.1 -10.1
1 0.0 0.0
3 -12.2 12.2
4 38.3 38.3
5 79.99 179.99
然后 do the same procedure as X 坐标。
Total complexity: 2nlogn + 4kn + klogk = O(nlogn)
k = 1 here.

【在 l*********8 的大作中提到】
: 如果要求O(n^2), 那么最naive办法都可以了。
: 如果要快些的话,好像可以用Quad-tree.

avatar
d*o
4
你总要计算任意两个pair之间的值吧,这样就是O(n^2)
如果比上面的要小,倒是可以计算任意点到原点的距离,排序一下,排除所有与该点距
离之差大于与最小距离之和的点,然后再找最小距离点,不过最坏情况也是O(n^2)
avatar
f*i
5
K-d tree? nlgn.
avatar
l*i
6
Jon Bentley's phd dissertation from UNC has the solution. Honestly it is way
too difficult for an interview. the problem is called all pair nearest
neighbor or simply All NN. Facebook puzzle used to have such a problem but
for that one you can get Accepted by some pruning in search.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。