Find the Closest intersection point in plan

前端 未结 3 1675
遥遥无期
遥遥无期 2021-02-09 09:50

I was asked following question in interview recently:

Let suppose you have, following grid on Cartesian coordinate system ( Quadrant I).

o - x - x - x          


        
3条回答
  •  名媛妹妹
    2021-02-09 10:24

    Before going to study k-d trees these are some thoughts that came to my mind:

    1. Iterate every point of your matrix, graph, or whatever it is
    2. Assign to each point (x,y) a value representing the MAX_distance from the Point to any of the people. (See a clarification example below)
    3. Take any of the points that have the lowest MAX_distance

    E.G. Given Point(0,0):

    • For (1,0) the distance is: 1
    • For (2,0) the distance is: 2
    • For (0,2) the distance is: 2
    • For (3,1) the distance is: 4
    • For (4,2) the distance is: 6

    The MAX_distance for (0,0) is 6. Given your input the lowest MAX_distance should be 3 and there are many Points with that value like (2,1) for instance.

    There should be ways to make this algorithm more efficient.. Maybe with k-d trees :p or with other tweaks like checking the total number of people, their relative location/distance, the MAX_distance value at any iteration, etc..

提交回复
热议问题