Find the Closest intersection point in plan

前端 未结 3 1781
遥遥无期
遥遥无期 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:20

    If the problem calls for minimizing the Manhattan distance (that is, people only walk parallel to the axes), then the problem is then pretty trivial. First, selecting the x coordinate and the y coordinate are independent problems.

    Then, for the each coordinate, simply find the median value of the position of the people along that axis. For many configurations of people, there can be more than one point that minimizes the sum of the walking distances of all people. (Just consider 2 people separated by more than 2 blocks in x and at the same y coordinate; any intersection in between will require the same total walking by the two people.)

    If the problem calls for minimizing the Euclidean distance, then the goal is to find the 2-variable L1 median. This is a standard problem, but it is far from trivial. (See here, for instance.) There is a unique answer. Given that this was an interview question, I suspect that this does not apply.

提交回复
热议问题