[LeetCode][Search] 296. Best Meeting Point

Problem

More LeetCode Discussions

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.

Solution

先找出所有的1的的坐标,然后枚举所有的格子,计算总距离,选择距离最小的那个。

class Solution {
public:
    int minTotalDistance(vector<vector<int>>& grid) {
        vector<pair<int, int> > points;
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[i].size(); j++) {
                if (grid[i][j] == 1) {
                    pair<int, int> p;
                    p.first = i;
                    p.second = j;
                    points.push_back(p);
                }
            }
        }
        
        int minDist = INT_MAX;
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[i].size(); j++) {
                int totalDist = 0;
                for(int k = 0; k < points.size(); k++) {
                    int dist = abs(i - points[k].first) + abs(j - points[k].second);
                    totalDist += dist;
                }
                minDist = min(minDist, totalDist);
            }
        }
        
        return minDist;
    }
};

LeetCode上更好的解答。从Hint的思路

Try to solve it in one dimension first. How can this solution apply to the two dimension case?

class Solution {
public:
    int minTotalDistance(vector<vector<int>>& grid) {
        vector<int> x;
        vector<int> y;
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[i].size(); j++) {
                if (grid[i][j] == 1) {
                    x.push_back(i);
                    y.push_back(j);
                }
            }
        }
        
        int minDist = 0;
        minDist += getMin(x);
        minDist += getMin(y);
        
        return minDist;
    }
    
    int getMin(vector<int> &a) {
        sort(a.begin(), a.end());
        
        int i = 0;
        int j = a.size() - 1;
        int dist = 0;
        while (i < j) {
            dist += a[j] - a[i];
            j--;
            i++;
        }
        
        return dist;
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,792评论 0 33
  • 隔了许久再次写文章,竟是内心如此的伤感,现在去远方的高铁上流着泪,思念我的姥娘,一个九十一岁的老人。 姥娘24岁生...
    不一样的心阅读 427评论 2 1
  • 银光凌波 孤舟漂泊 相思渡口难琢 剪一段时光 顾盼 凝眸 你的模样 心依旧 在曾经的源头 做一个梦的自由 把温情如...
    何夏陌舟阅读 249评论 0 0
  • - 我们第一次约会 是去陪他剪头发 他在楼下等我 我见到他 心里很紧张 就低着头走路 他笑着问我怎么这么害羞 - ...
    傻子的理想阅读 542评论 0 0
  • 时隔很久敲字码文。 简叔一条礼(qun)节(fa)性的消息发过来,问我是怎么知道这款app的。唔,愣了一秒,好像也...
    __方人也阅读 410评论 0 0