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. C与C++中的const

    同样,有下面一段代码: #include <iostream> using namespace std; int main() { ; int *j = (int *) &i; * ...

  2. Maven--生命周期和插件(四)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> <Maven—生命周期和插件(四)&g ...

  3. CSS介绍

    从HTML被发明开始,样式就以各种形式存在.不同的浏览器结合它们各自的样式语言为用户提供页面效果的控制.最初的HTML只包含很少的显示属性. 随着HTML的成长,为了满足页面设计者的要求,HTML添加 ...

  4. MongoDB 操作手冊CRUD插入

    插入操作 插入记录 1.插入一条记录 db.testData.insert({num:1,name:'a'}); 结果 WriteResult({ "nInserted" : 1 ...

  5. 【Linux】linux经常使用基本命令

    Linux中很多经常使用命令是必须掌握的,这里将我学linux入门时学的一些经常使用的基本命令分享给大家一下,希望能够帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显 ...

  6. 注册DLL,Unregister DLL

    前一篇文章,反复注册,反注册.... 写了一个小工具 怎么传图片感觉不对劲,StatusBar应改成 拖动DLL至上方 文件 http://files.cnblogs.com/magicdawn/Dl ...

  7. 使用WinAPI全局热键注册和全局模拟按键

    一.全局热键注册 1.先引用DLL [System.Runtime.InteropServices.DllImport("user32.dll")] //导入WinAPI publ ...

  8. EF中的约定

    优先级:Fluent API >数据注释>约定 CodeFirst约定 主键约定 如果类的属性名为"ID"(不区分大小写)或类名的后面跟有"ID", ...

  9. Android之Activity启动的源码简介

    从一个简单的startActivity开始 进入了Activity.java public void startActivity(Intent intent) { this.startActivity ...

  10. First 5 minutes of SQLite

    What is SQLite? SQLite is light-weight RDBMS, it is use the file system rather than the C/S client, ...