原题

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?

解析

曼哈顿距离:即一个矩阵的矩阵方格边线距离,如上例中,(0,0)-(2,2)的距离为4

该题求一个矩阵中的1位置到哪一个矩阵点的距离之和最小,并返回该距离

如上例中,三个点(0,0)(0,4)(2,2) 到(0,2)距离之和最小,为6

入参有两种表达:

一是入参为矩阵点的二维数组,每个数组值为0/1

二是入参为矩阵点的位置坐标,如上例中就是{{0,0},{0,4},{2,2}}

我的解法

没解出来,Hard难度的题真的不一样。。

我来写下我看过答案后的思路

原题的提示是将二维问题在一维先解决,再应用到二维上,意思就是先思考如何解决一条线上的点,求最短距离之和

1、一条线上有2个点

最短距离的点一定是这两个点的中点,则最短距离是两个点的直线距离

2、一条线上有3个点

最短距离是所有点到中间那个点的距离,还是最远的两个点的直线距离

3、一条线上有4个点

最短距离的点一定在最远的两个点之间,最远的两个点的距离就是固定的直线距离,那若要让中间两个点的距离最短,则最短距离点应该是中间两个点的中点,所以四个点的最短距离就是外部两点直线距离+内部两点直线距离

以此类推,一条线上的点,要求最短距离点,一定是中间两点的中点(偶数个点),或最中间的一个点(奇数个点),最短距离算法就是,最外侧两点距离+次外层两点距离+...+最内侧两点距离的和(若有中点,距离为0可以省略)

现在将问题扩展到二维,因为求的是曼哈顿距离,所以二维的最短距离也是在水平和垂直两个方向上的,所以只要求出水平的最短距离和垂直的最短距离,求和即可

最优解法

public class BestMeetingPoint {
//以下代码以入参为矩阵二维数组来实现
// O(MN)
public static int minTotalDistance(int[][] grid) {
//传入的矩阵是这样的 int[][] grid = { { 1, 0, 0, 0, 1 }, { 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0 } };
// means which row has the people on it
List<Integer> row = new LinkedList();
// means which col has the people on it
List<Integer> col = new LinkedList();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
row.add(i);
col.add(j);
}
}
}
// 分别放到一维上来做;
return getMin(row) + getMin(col);
} //一维距离算法
private static int getMin(List<Integer> list) {
int res = 0;
Collections.sort(list);
int i = 0, j = list.size() - 1;
while (i < j) {
res += list.get(j--) - list.get(i++);
}
return res;
}
}

【leetcode】296.Best Meeting Point的更多相关文章

  1. 【LeetCode】853. Car Fleet 解题报告(Python)

    [LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. [Scikit-learn] 2.5 Dimensionality reduction - Probabilistic PCA & Factor Analysis

    2.5.4. Factor Analysis PPCA的基本性质以及人肉推导: 以上假设z是标准正态分布的情况.以下是对z的分布的扩展,为general normal distribution. Fr ...

  2. 给mysql创建用户

    给mysql 创建用户过程: 1.进入mysql cmd下 2.输入密码:123 3.选择使用的数据库:use myeshop 4.创建新用户grant usage on *.* to 'cctvse ...

  3. Saltstack之使用salt安装es6.0以上的head插件

    本实验使用salt安装es6.0以上的head插件 ES6.0以上手动安装head插件参考:https://www.cnblogs.com/minseo/p/9117470.html 文件夹目录为 / ...

  4. Tracking of Features and Edges

    目录 Joint Tracking of Features and Edges Joint Tracking of Features and Edges 1. LK光流 基本LK光流运动假设: \[ ...

  5. 工具 --- Git使用

    创建远程仓库 Github 首相在GitHub网站创建一个仓库:右上角加号➕,选择new repository 然后创建编辑仓库:名称.说明.是否公开.语言.分支风格等信息.然后创建. 复制仓库地址 ...

  6. Apache Jmeter教程(一) - 入门

    一.下载Jmeter 登录官网Jmeter下载,得到压缩包jmeter-5.0.tgz,下载地址:http://jmeter.apache.org/download_jmeter.cgi. 二.安装 ...

  7. “但行好事 莫问前程 只问耕耘 不问收获 成功不必在我 而功力必不唐捐” 科技袁人·年终盛典——5G是科技时代非常重要的基础设施

    中国的科技实力:用数据对比展示当前中国整体科技实力在国际中的发展水平和未来的发展趋势. 主要分为基础研究和应用研究.其中基础研究通过论文数据进行对比展示,应用研究通过发明专利数据. 又分别结合当今中国 ...

  8. Linux 下清空或删除大文件内容的 5 种方法

    在 Linux 终端下处理文件时,有时我们想直接清空文件的内容但又不必使用任何 Linux 命令行编辑器 去打开这些文件.那怎样才能达到这个目的呢?在这篇文章中,我们将介绍几种借助一些实用的命令来清空 ...

  9. 【VS开发】IPicture在指定窗口绘制图

    1.利用IPicture接口加载.显示图片 IPicture接口管理一个图片对象和它的属性.图片对象提供对Bitmap Icon Metafile的语言不相关的抽象支持.图像对象的主要接口是IPict ...

  10. MySql中的count、NULL和空串的区别

    **1.count (1).count (*) 与 count (列名) 的区别** 表 count(1) count(*) count (列名) 作用 统计表中的所有的记录数 会统计表中的所有的记录 ...