[LeetCode] 296. Best Meeting Point 最佳开会地点
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:
- 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.
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 296. Best Meeting Point 最佳开会地点的更多相关文章
- [LeetCode] Best Meeting Point 最佳开会地点
A group of two or more people wants to meet and minimize the total travel distance. You are given a ...
- [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 ...
- 【leetcode】296.Best Meeting Point
原题 A group of two or more people wants to meet and minimize the total travel distance. You are given ...
- 296. Best Meeting Point
题目: A group of two or more people wants to meet and minimize the total travel distance. You are give ...
- 【LeetCode】253. Meeting Rooms II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+堆 日期 题目地址:https://leetco ...
- 【LeetCode】252. Meeting Rooms 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【leetcode】1229.Meeting Scheduler
题目如下: 你是一名行政助理,手里有两位客户的空闲时间表:slots1 和 slots2,以及会议的预计持续时间 duration,请你为他们安排合适的会议时间. 「会议时间」是两位客户都有空参加,并 ...
- [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 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 九、Spring之BeanFactory源码分析(一)
Spring之BeanFactory源码分析(一) 注意:该随笔内容完全引自https://blog.csdn.net/u014634338/article/details/82865644,写的 ...
- sierpinski地毯(II)
今天又是因为可以用py而高兴的一天. 继续咱的sierpinski地毯计划. 二,随机算法 在二十年前,磁盘容量以MB还是KB计的时候,分形解决计图的问题确实有很大的优势.存至多十来个数就好了.我要在 ...
- oracle排序子句的特殊写法与ORA-01785错误
刚刚写的SQL语句在执行的时候报[ORA-01785: ORDER BY item must be the number of a SELECT-list expression]错误,于是自己百度了一 ...
- WPF 3D相机基本坐标简介
基本概念 WPF中3D空间基本坐标系是右手坐标系. WPF中3D空间的原点是(0,0,0) Position: 这个参数用来表示相机在空间内的坐标.参数是(X,Y,Z).当修改相机的这个参数时,这个坐 ...
- Netty中的ChannelPipeline源码分析
ChannelPipeline在Netty中是用来处理请求的责任链,默认实现是DefaultChannelPipeline,其构造方法如下: private final Channel channel ...
- 从新手小白到老手大白的心路历程-First Blog
本人于2019年毕业重庆市某一所乡间大学,所学专业方向是.net,至今已经工作了1个多月了,天天被上司骂,还差点儿被开除,但我死皮赖脸的勉强的“活”了下来,在今后的日子里面,我会陆续的分享我的成长经历 ...
- Unable to parse composition
Unable to parse composition When i tried to load gif to my project suddenly return me error: Java.La ...
- Attract队作品简介
湖南师范大学 Attract队 荣获“航天宏图杯”PIE遥感图像处理软件二次开发大赛二等奖 作品名称:基于深度学习的遥感影像分类系统-以长沙市居住区分类为例 团队简介:周亮(指导老师).张慧(组长) ...
- mac os catalina mongodb最新安装流程
1.brew安装 不推荐用brew,因为现在mongodb闭源了,brew里已经搜索不到mongodb,不过还是可以用brew安装的,这篇就不写了. 2.官网下载 直接去官网下载一个zip,解压完放到 ...
- maven 学习---Maven构建自动化-Hudson
建立自动化定义场景,依赖项目建设过程中被启动,一旦项目生成成功完成,以确保相关的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序,网页UI和应用程序的桌面UI ...