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:

  1. Input:
  2.  
  3. 1 - 0 - 0 - 0 - 1
  4. | | | | |
  5. 0 - 0 - 0 - 0 - 0
  6. | | | | |
  7. 0 - 0 - 1 - 0 - 0
  8.  
  9. Output: 6
  10.  
  11. Explanation: Given three people living at (0,0), (0,4), and (2,2):
  12.   The point (0,2) is an ideal meeting point, as the total travel distance
  13.   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 距离,以此类推,直到最中间停止,那么一维的情况分析出来了,二维的情况就是两个一维相加即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int minTotalDistance(vector<vector<int>>& grid) {
  4. vector<int> rows, cols;
  5. for (int i = ; i < grid.size(); ++i) {
  6. for (int j = ; j < grid[i].size(); ++j) {
  7. if (grid[i][j] == ) {
  8. rows.push_back(i);
  9. cols.push_back(j);
  10. }
  11. }
  12. }
  13. return minTotalDistance(rows) + minTotalDistance(cols);
  14. }
  15. int minTotalDistance(vector<int> v) {
  16. int res = ;
  17. sort(v.begin(), v.end());
  18. int i = , j = v.size() - ;
  19. while (i < j) res += v[j--] - v[i++];
  20. return res;
  21. }
  22. };

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

解法二:

  1. class Solution {
  2. public:
  3. int minTotalDistance(vector<vector<int>>& grid) {
  4. vector<int> rows, cols;
  5. for (int i = ; i < grid.size(); ++i) {
  6. for (int j = ; j < grid[i].size(); ++j) {
  7. if (grid[i][j] == ) {
  8. rows.push_back(i);
  9. cols.push_back(j);
  10. }
  11. }
  12. }
  13. sort(cols.begin(), cols.end());
  14. int res = , i = , j = rows.size() - ;
  15. while (i < j) res += rows[j] - rows[i] + cols[j--] - cols[i++];
  16. return res;
  17. }
  18. };

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] Best Meeting Point 最佳开会地点的更多相关文章

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

  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] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  4. LeetCode 252. Meeting Rooms (会议室)$

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  5. [LeetCode] 253. Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  6. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  7. [LeetCode] Best Meeting Point

    Problem Description: A group of two or more people wants to meet and minimize the total travel dista ...

  8. [LeetCode#253] Meeting Rooms II

    Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...

  9. [LeetCode#252] Meeting Rooms

    Problem: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2] ...

随机推荐

  1. Oracle --> Vertica 数据类型转换规则

    需求:在Vertica数据库上建表,表结构来源于原Oracle数据库,故需要转换成Vertica数据库库表结构.   实际转换操作需要评估源库用到的所有数据类型和数据本身特性. 下面是总结的某场景下的 ...

  2. 由浅入深学习ajax跨域(JSONP)问题

    什么是跨域?说直白点就是获取别人网站上的内容.但这么说貌似又有点混淆,因为通常我们用ajax+php就可以获取别人网站的内容,来看下面这个例子. 来看看跨域的例子,jquery+ajax是不能跨域请求 ...

  3. 【十大经典数据挖掘算法】AdaBoost

    [十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 1. 集成学习 集成学习(ensem ...

  4. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  5. zeroclipboard浏览器复制插件使用记录

    一个简单例子: <html> <body> <button id="copy-button" data-clipboard-text="Co ...

  6. 通过向页面写html代码导出excel

    //excel文件名 string filename = "考勤汇总"; StringBuilder ExcelHtml = new StringBuilder(); ExcelH ...

  7. TypeSDK总体设计思路和架构

    引言:本文旨在提供读者制作一个自己的聚合SDK的思路,抛砖引玉,让更多的读者对聚合SDK有好的理解. 这是最好的时代,这是最坏的时代,这是智慧的时代,这是愚蠢的时代:这是信仰的时期,这是怀疑的时期:这 ...

  8. JavaScript简单分页,兼容IE6,~3KB

    简介 兼容IE6+及现代浏览器的简单分页,支持同一页面多个分页. 使用 Browser <link rel="stylesheet" href="css/GB-pa ...

  9. Tomcat 配置详解/优化方案

     转自:http://blog.csdn.net/cicada688/article/details/14451541 Service.xml Server.xml配置文件用于对整个容器进行相关的配置 ...

  10. 查看Oracle执行计划

    1.PL/SQL解释计划窗口 优点:方面 缺点:看到信息有限 2.explain_plan for 针对某个句子优化较方便 3.sqlplus Sqlplus里输入命令: set autotrace ...