Question

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.

Hint:

  1. Try to solve it in one dimension first. How can this solution apply to the two dimension case?

Solution 1 -- Sort

根据曼哈顿距离的表达式,我们可以把这个问题转化为求每一维的最短距离。

总距离 = x上最短总距离 + y上最短总距离

并且,我们观察到,对于一个序列,如 [0,0,1,0,1,0,1,1,1,0,0,1]

当邮局选在median的位置时,距离和是最小的。

因此,这里我们遍历数组,得到home的x和y坐标值,然后分别对这两个list排序。再用双指针得到总的最短距离和。

Time complexity O(mn log(mn))

 public class Solution {
public int minTotalDistance(int[][] grid) {
if (grid == null || grid.length < 1) {
return 0;
}
int m = grid.length, n = grid[0].length;
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
xList.add(i);
yList.add(j);
}
}
}
return calcTotalDistance(xList) + calcTotalDistance(yList);
} private int calcTotalDistance(List<Integer> list) {
if (list == null || list.size() < 2) {
return 0;
}
Collections.sort(list);
int len = list.size();
int i = 0, j = len - 1;
int result = 0;
while (i < j) {
int left = list.get(i);
int right = list.get(j);
result += (right - left);
i++;
j--;
}
return result;
}
}

Solution 2 -- Without sorting

Discussion里有人给出了不用排序的方法。时间复杂度降低为O(mn)

因为双层循环遍历数组本身就是有次序性的。

1. 外层循环为遍历行,内层循环为遍历该行的每一个元素。

这样得到的是排序好的x的list

2. 外层循环为遍历列,内层循环为遍历该列的每一个元素。

这样得到的是排序好的y的list

 public class Solution {
public int minTotalDistance(int[][] grid) {
if (grid == null || grid.length < 1) {
return 0;
}
int m = grid.length, n = grid[0].length;
List<Integer> xList = new ArrayList<Integer>();
List<Integer> yList = new ArrayList<Integer>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
xList.add(i);
}
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
if (grid[i][j] == 1) {
yList.add(j);
}
}
} return calcTotalDistance(xList) + calcTotalDistance(yList);
} private int calcTotalDistance(List<Integer> list) {
if (list == null || list.size() < 2) {
return 0;
}
int len = list.size();
int i = 0, j = len - 1;
int result = 0;
while (i < j) {
result += (list.get(j--) - list.get(i++));
}
return result;
}
}

Best Meeting Point 解答的更多相关文章

  1. 【Alpha阶段】第五次 Scrum Meeting

    每日任务 1.本次会议为第 五次 Meeting会议: 2.本次会议在上午09:35,大课间休息时间在陆大召开,召开本次会议为20分钟,汇报自己的任务和讨论接下来的任务: 一.今日站立式会议照 二.每 ...

  2. 第五次Scrum meeting

    第五次Scrum meeting 会议内容: 连接方面:确定封装成json的文本格式,尽量在满足在线组和手机客户端两组的情况下,降低自身的难度 测试方面:进行新一轮测试,主要测试程序的稳定性和可靠性, ...

  3. 第四次Scrum meeting

    第四次Scrum meeting 会议内容: 沟通方面:与学霸在线组.学霸手机客户端组进行沟通,了解现阶段各个小组的进度,并针对接口结构方面进行调整 前后端:我们完全可以是不需要界面的,但是为了用户的 ...

  4. 第三次Scrum meeting

    第三次Scrum meeting 会议主要内容: 测试方面:确定测试的各个环节以及测试的相关要求,完成初步的功能测试.同时在测试时仔细记录相应错误信息,并进行备注. 沟通方面:同Dream团队(学霸前 ...

  5. 【LeetCode】253. Meeting Rooms II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...

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

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

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

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

  8. [LeetCode] Meeting Rooms 会议室

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

  9. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

随机推荐

  1. FreeBSD ipfw App配置Examples

    Step # 1: Enabling IPFW Open /etc/rc.conf file # vi /etc/rc.conf Append following settings: firewall ...

  2. poj 2407 Relatives(简单欧拉函数)

    Description Given n, a positive integer, how many positive integers less than n are relatively prime ...

  3. 转:DesiredCapabilities内容详解--Appium服务关键字

    ## Appium 服务关键字 <expand_table> |关键字|描述|实例| |----|-----------|-------| |`automationName`|你想使用的自 ...

  4. windows 查看端口被占用

    C:\Users\xxxx> 根据端口找到进程14716 C:\Users\xxxx>tasklist|findstr "14716"node.exe 14716 Co ...

  5. Spring3 MVC 之 Hello Word

    开发工具: MyEclipse  10.0 项目目录: [http://www.cnblogs.com/rhythmK/] 1.新建项目:File->New->Web Project  项 ...

  6. 漫话Unity3D(一)

    前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...

  7. LDAP禁止匿名访问

    LDAP默认是允许用户匿名访问的,如下图:在使用工具连接时,勾选匿名绑定后,不需要输入UserDN和密码就可能连接到LDAP服务器,但是只能进行read及search操作.不能做任何的修改及删除操作. ...

  8. HTML与CSS入门——第七章 使用表格显示信息

    知识点: 1.创建简单表格的方法 2.控制表格大小的方法 3.对齐内容及在表格中跨越行和列的方法 7.1 创建简单的表格: table标签,border控制边框 tr标签,创建表格的行,包含td td ...

  9. asp.net中使用forms验证

    1.首先在web.config中修改验证方式为 "Forms" <authentication mode="Forms"> 这里的模式有很多中,可自 ...

  10. linux oracle 设置随系统自动启动数据库实例和监听

    在root账户下修改/etc/oratab 文件: # vi /etc/oratab 找到orcl=/db/app/oracle/product/11.1.0/db_1 :N这一行 改为: orcl= ...