Best Meeting Point

A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.

For example, given three people living at (0,0)(0,4), and(2,2):

1 - 0 - 0 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0

The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6.

分析:

  第一反应就是求二维平面上的距离最优值;由于是曼哈顿距离,所以x维和y维可以分开求最小,最终结果也会最小,这样转化成了一维轴上绝对值之和最小,中学学过嘛,画图可知。

解法:

  找到x轴上所有为1的点,然后从外到内依次两两index求差,这些差的总和为x轴上的最小值;y轴上也做同样的操作得到y轴最小值。两轴上的最小值之和为最小曼哈顿距离。时间复杂度为m*n,空间复杂度为1的个数。

代码:

int minDist(vector<vector<int> > v) {
deque<int> inum, jnum;
int dist = ;
for(int i = ; i < v.size(); i++) {
for(int j = ; j < v[].size(); j++) {
if(v[i][j])
inum.push_back(i);
}
}
while(inum.size() >= ) {
dist += inum.back() - inum.front();
inum.pop_front();
inum.pop_back();
}
for(int j = ; j < v[].size(); j++) {
for(int i = ; i < v.size(); i++) {
if(v[i][j])
jnum.push_back(j);
}
}
while(jnum.size() >= ) {
dist += jnum.back() - jnum.front();
jnum.pop_front();
jnum.pop_back();
}
return dist;
}

[Locked] Best Meeting Point的更多相关文章

  1. [Locked] Meeting Room I && II

    Meeting Room Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2 ...

  2. ORA-28000: the account is locked 账户被锁

    这种情况可能是因为你输入错误的用户名密码达到10次,oracle给你锁住了. 解决方法: 首先 ~bash$ sqlplus /nolog SQL> conn sys/sys as sysdba ...

  3. [LeetCode] Best Meeting Point 最佳开会地点

    A group of two or more people wants to meet and minimize the total travel distance. You are given a ...

  4. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  6. Scrum Meeting 20161205

    本周Sprint Master 史少帅 一. 会议概要 作为一个新的sprint的开端,本次scrum meeting总结了每个人过去以来的工作,并明确了下一步的计划,具体如下: 工作总结: · 陈双 ...

  7. Beta阶段第十次Scrum Meeting

    情况简述 BETA阶段第十次Scrum Meeting 敏捷开发起始时间 2017/1/4 00:00 敏捷开发终止时间 2017/1/5 00:00 会议基本内容摘要 deadline到来 参与讨论 ...

  8. Beta阶段第九次Scrum Meeting

    情况简述 BETA阶段第九次Scrum Meeting 敏捷开发起始时间 2017/1/2 00:00 敏捷开发终止时间 2017/1/3 00:00 会议基本内容摘要 deadline临近 参与讨论 ...

  9. Beta阶段第八次Scrum Meeting

    情况简述 BETA阶段第八次Scrum Meeting 敏捷开发起始时间 2016/12/21 00:00 敏捷开发终止时间 2016/12/22 00:00 会议基本内容摘要 deadline临近 ...

随机推荐

  1. DataTable和List集合互转

    /// <summary> /// 将集合转换成DataTable /// </summary> /// <param name="list"> ...

  2. javascript-图片横向无缝隙滚动

    <style type="text/css"> <!-- ul,li,div{margin:0; padding:0; font-size:12px;} #dem ...

  3. xml中报错,验证是否是xml报错

    1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[                  ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...

  4. oracle批量导入数据

    关键代码 OracleDataAdapter da=new OracleDataAdapter(); string sql_select = string.Format("select id ...

  5. 【HOJ1356】【Miller_rabin素性测试】Prime Judge

    Given a positive integer, your job is writing a program to determine whether it is a prime number or ...

  6. lmsw - 加载机器状态字

    将源操作数加载到机器状态字,即寄存器 CR0 的位 0 到 15.源操作数可以是 16 位通用寄存器或内存位置.只有源操作数的低 4 位(也就是 PE.MP.EM 及 TS 标志)会加载到 CR0.C ...

  7. 解决IE6不支持position:fixed;的问题

    在网页设计中,时常要用到把某个元素始终定位在屏幕上,即使滚动浏览器窗口也不会发生变化. 一般我们会使用position:fixed来进行绝对固定,但IE6并不支持position:fixed属性,所以 ...

  8. [C#]获取最近在Windows上所使用的文件

    class RecentlyFileHelper { public static string GetShortcutTargetFile(string shortcutFilename) { var ...

  9. dedecms 文章排列方式

    orderby='sortrank' 文档排序方式orderby='hot' 或 orderby='click' 表示按点击数排列orderby='sortrank' 或 orderby='pubda ...

  10. opencv数据结构总结

    OpenCV里面用到了很多图像相关的数据结构,熟练掌握它们是学习图像的基础. 1.IplImage IplImage IplImage IPL 图像头 typedef struct _IplImage ...