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|.

Example:

Input: 

1 - 0 - 0 - 0 - 1
| | | | |
0 - 0 - 0 - 0 - 0
| | | | |
0 - 0 - 1 - 0 - 0 Output: 6 Explanation: Given three people living at (0,0), (0,4), and (2,2):
  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?

这道题让我们求最佳的开会地点,该地点需要到每个为1的点的曼哈顿距离之和最小,题目中给了提示,让从一维的情况来分析,先看一维时有两个点A和B的情况,

______A_____P_______B_______

可以发现,只要开会为位置P在 [A, B] 区间内,不管在哪,距离之和都是A和B之间的距离,如果P不在 [A, B] 之间,那么距离之和就会大于A和B之间的距离,现在再加两个点C和D:

______C_____A_____P_______B______D______

通过分析可以得出,P点的最佳位置就是在 [A, B] 区间内,这样和四个点的距离之和为AB距离加上 CD 距离,在其他任意一点的距离都会大于这个距离,那么分析出来了上述规律,这题就变得很容易了,只要给位置排好序,然后用最后一个坐标减去第一个坐标,即 CD 距离,倒数第二个坐标减去第二个坐标,即 AB 距离,以此类推,直到最中间停止,那么一维的情况分析出来了,二维的情况就是两个一维相加即可,参见代码如下:

解法一:

class Solution {
public:
int minTotalDistance(vector<vector<int>>& grid) {
vector<int> rows, cols;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == ) {
rows.push_back(i);
cols.push_back(j);
}
}
}
return minTotalDistance(rows) + minTotalDistance(cols);
}
int minTotalDistance(vector<int> v) {
int res = ;
sort(v.begin(), v.end());
int i = , j = v.size() - ;
while (i < j) res += v[j--] - v[i++];
return res;
}
};

我们也可以不用多写一个函数,直接对 rows 和 cols 同时处理,稍稍能简化些代码:

解法二:

class Solution {
public:
int minTotalDistance(vector<vector<int>>& grid) {
vector<int> rows, cols;
for (int i = ; i < grid.size(); ++i) {
for (int j = ; j < grid[i].size(); ++j) {
if (grid[i][j] == ) {
rows.push_back(i);
cols.push_back(j);
}
}
}
sort(cols.begin(), cols.end());
int res = , i = , j = rows.size() - ;
while (i < j) res += rows[j] - rows[i] + cols[j--] - cols[i++];
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/296

类似题目:

Minimum Moves to Equal Array Elements II

Shortest Distance from All Buildings

参考资料:

https://leetcode.com/problems/best-meeting-point/

https://leetcode.com/problems/best-meeting-point/discuss/74186/14ms-java-solution

https://leetcode.com/problems/best-meeting-point/discuss/74244/Simple-Java-code-without-sorting.

https://leetcode.com/problems/best-meeting-point/discuss/74193/Java-2msPython-40ms-two-pointers-solution-no-median-no-sort-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 296. Best Meeting Point 最佳开会地点的更多相关文章

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

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

  2. [Swift]LeetCode296. 最佳开会地点 $ Best Meeting Point

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

  3. 【leetcode】296.Best Meeting Point

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

  4. 296. Best Meeting Point

    题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...

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

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

  6. 【LeetCode】252. Meeting Rooms 解题报告(C++)

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

  7. 【leetcode】1229.Meeting Scheduler

    题目如下: 你是一名行政助理,手里有两位客户的空闲时间表:slots1 和 slots2,以及会议的预计持续时间 duration,请你为他们安排合适的会议时间. 「会议时间」是两位客户都有空参加,并 ...

  8. [LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离

    You want to build a house on an empty land which reaches all buildings in the shortest amount of dis ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. MySQL管理工具 -- MySQL Workbench

    管理MySQL,可以使用可视化图形界面MySQL Workbench.MySQL Workbench是一个图形客户端,可以用可视化的方式查询.创建和修改数据库表.它对MySQL的操作仍然是发送SQL语 ...

  2. linux 修改文件的时间属性

    二.修改文件时间 创建文件我们可以通过touch来创建.同样,我们也可以使用touch来修改文件时间.touch的相关参数如下: -a : 仅修改access time. -c : 仅修改时间,而不建 ...

  3. C#之初识异步

    什么是异步 举个例子:小明的妈妈让小明烧一壶水,水烧开后要倒进水壶里,同时还需要把家里打扫一下. 小明的操作流程一:烧水---->等待至水烧开----->水倒进水壶里--------> ...

  4. Python基础18

    “为什么有列表,还要元组?” 1. 元组可看成是简单的对象组合,而列表是随时间改变的数据集合. 2. 元组的不可变特性提供了某种完整性,确保元组不会被另一个引用来修改.类似于其它语言中的常数声明.

  5. Python进阶----异步同步,阻塞非阻塞,线程池(进程池)的异步+回调机制实行并发, 线程队列(Queue, LifoQueue,PriorityQueue), 事件Event,线程的三个状态(就绪,挂起,运行) ,***协程概念,yield模拟并发(有缺陷),Greenlet模块(手动切换),Gevent(协程并发)

    Python进阶----异步同步,阻塞非阻塞,线程池(进程池)的异步+回调机制实行并发, 线程队列(Queue, LifoQueue,PriorityQueue), 事件Event,线程的三个状态(就 ...

  6. php 除10取整,取十位数前面一个数字,百位前两个数字

    需求:php 除10取整,取十位数前面一个数字,百位前两个数字,并把大于2的加红显示 例:0-9,10-19,20-29,30-39,110-119对应为:0 1 2 3 11 实现主要方法:$num ...

  7. SQL行转列,列转行

    SQL 行转列,列转行 行列转换在做报表分析时还是经常会遇到的,今天就说一下如何实现行列转换吧. 行列转换就是如下图所示两种展示形式的互相转换 行转列 假如我们有下表: SELECT * FROM s ...

  8. Jenkins 打tag回滚

    利用jenkins,从gitlab上拉取代码,然后发布,如果想进行代码回退,其实还是代码发布,拉取的时候,选择合适的标签. 一.利用Git parameter插件选择branch或tag.下面的文本参 ...

  9. GCN 简单numpy实现

    `#参考:https://blog.csdn.net/weixin_42052081/article/details/89108966 import numpy as np import networ ...

  10. 《高性能 Go 代码工坊》中译

    深入研究 Go 应用性能提升的英语系列文章,这里是中译 https://www.yuque.com/ksco/uiondt