`

Google Interview - Find Meeting Point (Manhattan distance)

 
阅读更多

I. Manhattan distance

In a city there are N persons. A person can walk only horizontally or vertically. Find a point that minimizes the sum of distances all persons walk to the point.

This is called the manhattan distance since a person can walk only horizontally or vertically, like in the city of Manhattan.

Let's assume this is 1-dimensional. Then for 2 persons, the point can be any point on the line connecting the 2 persons. For 3 persons , the point is the middle person. For 4 persons, the point can be any point between the middle 2 persons. For 5 persons, the point is the middle person. In general, for odd number of persons, it's the person in the middle; for even number of persons, it's any point between the middle 2 persons.

This can be extended to 2-dimensional. The answer is the point (x, y), where x and y are the median points taken independently of all the xi and yi for i = 1 to n. [1][2]

 

The cool thing about the Manhatan distance is that the distance itself comprises of two independent components: the distance on the x and y coordinate. Thus you can solve two simpler tasks and merge the results from them to obtain the desired results.

The task I speak of is: given are points on a line. Find the point on the line that minimizes the sum of the absolute distances to all the points. If there are many find all of them (btw they always turn to be a single segment which is easy to prove). The segment is determined by the (potentially two) points medians of the set. By median I mean the point that has equal number of points to the left and to the right of it. In case the number of points is odd there is no such point and you choose the points with difference 1 in both directions to form the segment.

Here I add examples of solutions of this simpler task:

In case the points on the line are like that:

-4 | | | 0 | 2 3 4
             ^

The solution is just a point and it is 2.

In case the points on the line are like that:

-4 | | | 0 | 2 3
         ^---^

The whole segment [0, 2] is the solution of the problem.

You solve this task separately for the x and y coordinate and then merge the results to obtain the rectangle of minimum distanced points.


EXAMPLE

And now comes an example of the solution for the initial task.

Imagine you want to find the points that are with minimum Manhatan distance to the set (0, 6), (1, 3), (3, 5), (3, 3), (4, 7), (2, 4)

You form the two simpler tasks:

For x:

0 1 2 3 3 4
    ^-^

And here the solution is the segment [2, 3] (note that here we have duplicated point 3, which I represented in probably not the most intuitive way).

For y:

3 3 4 5 6 7
    ^-^

Here the solution is the segment [4, 5].

Finally we get that the solution of the initial task is the rectangle with formula:

 2 <= x <= 3; 4 <= y <= 5 

COMPLEXITY

As many people show interest in this post I decided to improve it a bit more.

Let's speak about complexity.

The complexity of the task is actually the same as the complexity of solving the simpler task (because as already discussed the solution actually consists of solving two simpler tasks). Many people will go and solve it via sorting and then choosing out the medians. However, this will causeO(nlog n) complexity, where n is the number of points in the input set.

This can be improved if a better algorithm for finding the kth element is used (Example of implementation in the C++ STL). This algorithm basically follows the same approach as qsort. The running time is O(n). Even in the case of two median points this will still remain linear (needing two runs of the same algorithm), and thus the total complexity of the algorithm becomes O(n). It is obvious that the task can not be solved any faster, as long as the input itself is of the mentioned complexity.

 

注意:

还有可能出现中间有障碍物的情况。应该特殊考虑!例如Google曾经的一道面试题是(来自这里):

有一个gym,用block表示。里面有健身器材,还有障碍物。让找一个最佳的位置放置椅子,使得椅子到所有健身器材的曼哈顿距离最短。


II. Geometric median.

The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing the sum of distances to the sample points [3]. This no longer requires the path be horizontal or vertical.

Despite the simple form, the solution is much more complex than the similar problem of finding the center of mass, which minimizes the sum of the squares of distances of the points to the center. There is no simple formula for the solution.

For 2 points, it's any point on the line connecting the 2 points.

For 3 non-collinear points, the problem is known as Fermat's problem. Solution is in [3]. For 4 co-planar points, the solution is also in [3].

For more points, the solution can be approximated by numerical methods such as the Weiszfeld's algorithm.

References:
[1] Shortest distance travel - common meeting point
[2] Algorithm to find point of minimum total distance from locations
[3] Geometric median

分享到:
评论

相关推荐

    X1000_HALLEY2_BASEBOARD_V4.1开发资料.docx

    基于君正x1000e开发板的Manhattan平台编译系统应用层开发环境搭建和串口测试(IIC、SPI、UART) 一、Manhattan平台编译系统 Manhattan平台编译系统是君正提供的针对x1000系列芯片的系统开发工具,提供了更加简单...

    knn.rar_K._K近邻 距离_K近邻算法_manhattan distance_曼哈顿距离

    **K. K近邻算法详解** K近邻(K-Nearest Neighbors,简称KNN)是一种简单且直观的监督学习算法,常用于分类和回归问题。它的基本思想是:对于给定的一个新实例,我们将其与训练集中所有的实例进行比较,找出与其最...

    Interview-Coding:面试编码

    导航到Manhattan.java文件的位置,并使用以下命令编译该程序: javac Manhattan.java 接下来,通过执行以下命令运行程序: java Manhattan 用法 执行完程序后,将为您提供该程序的简短说明和提示输入第一个坐标点的...

    机器学习--KNN算法.pptx

    - 曼哈顿距离(Manhattan Distance, L1):在各坐标轴上绝对差的总和,适合于各特征不需归一化的场景。 - 明氏距离(Minkowski Distance, Lq):当q=1时,即为曼哈顿距离;当q=2时,即为欧氏距离。选择合适的距离...

    matlab代码cox-Shortest-path-distance-MPLCP:最短路径距离

    Manhattan Poisson line Cox process中最短的路径距离”,2020,可在线获取:。 运行“ sim_thy_shortestpath_typical_intersection.m”以计算从MPLP的典型交点到MPLCP的最近点的最短路径距离的CDF(在路径距离意义...

    manhattan-distance:计算两个数组之间的曼哈顿(城市街区)距离

    用法 var manhattan = require ( 'compute-manhattan-distance' ) ;曼哈顿( x, y[, accessor] ) 计算两个数组之间的。 var x = [ 2 , 4 , 5 , 3 , 8 , 2 ] ,y = [ 3 , 1 , 5 , - 3 , 7 , 2 ] ;var d = manhattan ( x...

    Python库 | manhattan_chains-0.0.6.tar.gz

    distance = manhattan_chains.manhattan_distance(point1, point2) print(distance) # 输出:7 ``` 总的来说,"manhattan_chains"库可能是用来处理与曼哈顿距离相关的算法或数据结构的工具,它提供了一种方便的方式...

    MahNMF Manhattan Non-negative Matrix Factorization

    MahNMF Manhattan Non-negative Matrix Factorization code % Manhattan Non-negative Matrix Factorization. % ManhNMF: Matlab Code for Efficient Robust Manhattan NMF Solver % Reference % [1] N. Guan, D....

    程序员为什么还要刷题-intro-to-tdd-rspec-and-learn-manhattan-fasttrack-111018:tdd-

    程序员常刷题TDD、RSpec 和 Learn 介绍 目标 定义代码测试的目的。 阅读 RSpec 测试。...通过learn命令运行测试。...背后的基本思想是,在开始编码之前,您应该考虑您希望程序做什么以及您希望代码如何运行。...

    程序员为什么还要刷题-rspec-fizzbuzz-manhattan-fasttrack-111018:rspec-fizzbuzz-man

    程序员常刷题目标 构建利用流量控制的方法 阅读并理解测试输出以开发工作程序 更加熟悉测试驱动开发的概念 关于本指南的说明 我们之前已经了解了测试驱动开发以及阅读和理解 RSpec 测试的概念。...

    DataMining-ch2.pdf

    通过推荐系统的例子讲解数据挖掘,简单易懂,图文并茂。The greater the r, the more a large difference in one dimension will influence the total difference.

    Manhattan-Grid:每个人都可以使用的网格系统,带有React的绑定

    npm install --save manhattan 通过加载器,css前置/后置处理器等添加CSS。 使用简单CSS类组成或通过我们的React绑定(可让您在标记中组成网格)来构建组件树。 例子 import { Cell , Container } from 'manhattan...

    Cracking the PM Interview

    Cracking the PM InterviewHow many pizzas are delivered in Manhattan? How do you design an alarm clock for the blind? What is your favorite piece of software and why? How would you launch a video ...

    Ford-Credit-Co:下面,我为福特信用实习创建了两个练习

    在Ford-Credit-Co文件夹中找到ManhattanDistance.py文件,然后运行以下命令: python ManhattanDistance.py如果您已安装python3,请执行以下操作: python3 ManhattanDistance.py然后,您可以按照屏幕上的说明进行...

    包装物流管理软件:Manhattan Associates二次开发-ManhattanAssociates包装物流管理软件概述

    包装物流管理软件:Manhattan Associates二次开发_ManhattanAssociates包装物流管理软件概述.docx 包装物流管理软件:Manhattan Associates二次开发_Manhattan软件架构与组件.docx 包装物流管理软件:Manhattan ...

    PyPI 官网下载 | manhattan_assets-0.5.0.tar.gz

    **PyPI 官网下载 | manhattan_assets-0.5.0.tar.gz** 在Python的开发环境中,`PyPI`(Python Package Index)是至关重要的一个资源库,它提供了大量预封装的Python软件包,方便开发者下载和安装。标题中的"PyPI ...

Global site tag (gtag.js) - Google Analytics