Best Meeting Point 解答
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:
- 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 解答的更多相关文章
- 【Alpha阶段】第五次 Scrum Meeting
每日任务 1.本次会议为第 五次 Meeting会议: 2.本次会议在上午09:35,大课间休息时间在陆大召开,召开本次会议为20分钟,汇报自己的任务和讨论接下来的任务: 一.今日站立式会议照 二.每 ...
- 第五次Scrum meeting
第五次Scrum meeting 会议内容: 连接方面:确定封装成json的文本格式,尽量在满足在线组和手机客户端两组的情况下,降低自身的难度 测试方面:进行新一轮测试,主要测试程序的稳定性和可靠性, ...
- 第四次Scrum meeting
第四次Scrum meeting 会议内容: 沟通方面:与学霸在线组.学霸手机客户端组进行沟通,了解现阶段各个小组的进度,并针对接口结构方面进行调整 前后端:我们完全可以是不需要界面的,但是为了用户的 ...
- 第三次Scrum meeting
第三次Scrum meeting 会议主要内容: 测试方面:确定测试的各个环节以及测试的相关要求,完成初步的功能测试.同时在测试时仔细记录相应错误信息,并进行备注. 沟通方面:同Dream团队(学霸前 ...
- 【LeetCode】253. Meeting Rooms II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...
- [LeetCode] Best Meeting Point 最佳开会地点
A group of two or more people wants to meet and minimize the total travel distance. You are given a ...
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
随机推荐
- 页面onclick()中传值问题
html中onclick()里面传变量到javascript中的问题,小小的记录下: 传变量的话一定要加 '' <span onclick="sellGoods('${session ...
- hdu 1695 GCD(欧拉函数+容斥)
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- Web Service工作原理
Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的 ...
- BNU10806:请在此处签到
每年圣诞,ZUN都会邀请很多人到幻想乡举行联欢,今年也不例外.在联欢前,所有人需要在自己的昵称旁签到(签全名),以示出席.然后ZUN 会把大家的签到表保存下来作为纪念,以激励来年努力工作. 昵称: ...
- [Redux] Passing the Store Down with <Provider> from React Redux
Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...
- jQuery.extend 和 jQuery.fn.extend
1.jQuery.extend 我们先把jQuery看成了一个类,这样好理解一些.jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑 ...
- Linux基础知识笔记
1.case的命令格式 #!/bin/sh echo "please input number 1 to 3" read number case $number in ) e ...
- ASP.NET 动态编译、预编译和 WebDeployment 项目(转)
概述 在 Web 服务器上,既可以部署源文件,也可以部署编译后程序集. 若部署源文件,则当用户访问时,Web 应用程序会被动态编译,并缓存该程序集,以便下次访问. 否则,若部署程序集,Web 应用程序 ...
- String功能测试
package foxe; import java.io.IOException;import java.util.StringTokenizer; public class MainClass { ...
- c++11-bind的用法
bind函数 在c++11之前,要绑定某个函数.函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st.bind2nd.fun_ptr.mem_fun和mem_fun_ref等.在c+ ...