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. WisDom.Net 框架设计(二) 服务总线

    WisDom.Net 框架设计--服务总线 1.Soa 简介     soa 就是面向服务的体系结构 是一个组件模型,不同的组件之间通过定义良好的接口联系起来.就像盖房子一块砖头一块砖头的砌墙,一片一 ...

  2. WPF Paragraph获取或修改文本内容

    一.说明 Paragraph继承自Block,Block继承自TextElement,在TextElement中 // // 摘要: // 获取表示元素中内容末尾的 System.Windows.Do ...

  3. Prototype 模式

    Prototype 模式提供了一个通过已存在对象进行新对象创建的接口(Clone) ,Clone()实现和具体的实现语言相关,在 C++中我们将通过拷贝构造函数实现之. /////////////// ...

  4. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  5. 《vi中的替换艺术》-linux命令五分钟系列之十一

    vi方面的内容不知道分类到哪里好,就放到<Linux命令五分钟系列>里吧! 今天编程,关于栈的一个小例子,其间我需要把”S.”替换为”S->”(替换不包括双引号). 其实这个不难,不 ...

  6. underscorejs-sample学习

    2.22 sample 2.22.1 语法: _.sample(list, [n]) 2.22.2 说明: 从list中产生一个随机样本.传参n后返回n个随机元素,各元素不重复. 2.22.3 代码示 ...

  7. Oracle 面试宝典 - General Questions

    转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...

  8. 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。

    源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...

  9. 防止mysql注入

    function check($sql_str) { $checks=eregi('select|insert|update|delete|\'|\/|\\\|\*|\.|union|into|loa ...

  10. 局部变量存储区域静态变量存储区域static变量存储区域

    局部变量存储区域静态变量存储区域static变量存储区域 常见的存储区域可分为: 1.栈 由编译器在需要的时候分配,在不需要的时候自动清楚的变量的存储区.里面的变量通常是局部变量.函数参数等. 2.堆 ...