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. [教程]隐藏ActionBar中的MenuItem

    有时候我们需要在不同的时候改变ActionBar中MenuItem的项数,或者隐藏某些MenuItem,百度上找了很久没什好资料,还是Google了一下,StackOverFlow上有大神解决了. 先 ...

  2. mysql 从data文件恢复数据库

    安装在D:\mysql\mysql-5.6.24-winx64下的mysql 由于系统坏了,移到另外一台机器上启动 步骤如下 1.复制以前的mysql安装文件及data文件下:2.全新安装mysql3 ...

  3. 搭建Android环境

    1.相关文件下载: 1.1.Java jdk下载: JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jre7-downl ...

  4. [Angular 2] WebStorm - Managing Imports

    Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...

  5. Smack+Openfire 接收和发送文件

    转载请注明出处:http://blog.csdn.net/steelychen/article/details/37958839 发送文件须要提供准确的接收放username称(例:user2@192 ...

  6. FineUI控件之树的应用(二)

    一.Tree控件应用 <f:PageManager ID="PageManager1" runat="server" /> <f:Tree I ...

  7. C++中使用stringstream简化类型转换

    C++标准库中的<sstream>提供了一个stringstream,以前基本没用过,突然发现很好用(^-^)V 参见 http://www.cplusplus.com/reference ...

  8. 使用python发邮件

    使用python发邮件 网上有很多发邮件的例子,本人在网上找了一份,稍加修改后使用 上源码 # encoding=utf-8 from email.mime.image import MIMEImag ...

  9. Java中,&&与&;||与|的区别

    我们很多人在学习Java的时候,或者其他语言(如:C#,.Net等)都会遇到&和&&,|和||.然而,如果你没有真正理解他们的意思,这回给你的思路上带来很大的麻烦.在Java的 ...

  10. 保存BASE64编码图片

    1.前端上传用户图片时,一些K数较小图片,头像图标等 .以bass64编码后的字符串传到服务器. 2.服务器接收并保留到本地. // 页面上点击保存 $.post('/imgupload/save', ...