原题

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. jemter 90%line的解释

    假如: 有10个数: 1.2.3.4.5.6.7.8.9.10    按由大到小将其排列. 求它的第90%百分位,也就是第9个数刚好是9 ,那么他的90%Line 就是9 . 另一组数: 2.2.1. ...

  2. Hadoop常用命令介绍

    本文主要介绍 Hadoop 常用的命令. test命令 用于检测文件或目录是否存在,判断文件或目录大小. -test -[defsz] <path> : Answer various qu ...

  3. [C++]单源最短路径:迪杰斯特拉(Dijkstra)算法(贪心算法)

    1 Dijkstra算法 1.1 算法基本信息 解决问题/提出背景 单源最短路径(在带权有向图中,求从某顶点到其余各顶点的最短路径) 算法思想 贪心算法 按路径长度递增的次序,依次产生最短路径的算法 ...

  4. winform 更新文件上传(一)

    using Common; using DevExpress.XtraEditors; using FileModel.UpLoad; using System; using System.Colle ...

  5. ffmpeg学习笔记-音频解码

    在之前的文章已经初步对视频解码有个初步的认识了,接下来来看一看音频解码 音频解码步骤 音频解码与视频解码一样,有者固有的步骤,只要按照步骤来,就能顺利的解码音频 以上是ffmpeg的解码流程图,可以看 ...

  6. 最新 网龙网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.网龙网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了网龙网络公司.6.7月主要是做系统复习.项目复盘.Le ...

  7. flex布局时,vertical-align:middle无效

    flex布局,子元素内部vertical-align=middle无效,对文字的容器 display: flex; align-items: center;

  8. 关于element中的父子组件的传值问题

    首先讲一下父子组件的传值问题. 这是大家很熟悉的一张图,讲述了父子组件传值的过程.父组件通过prop传值给子组件,子组件通过$emit给父组件发送消息来使父组件的prop发生变化.这都是老生常谈了.下 ...

  9. [学习笔记] 在Eclipse中导出可以直接运行的jar,依赖的jar在子目录中

    工程创建可参考前文: [学习笔记] 在Eclipse中使用Hibernate,并创建第一个工程,数据库为Oracle XE 在工程上鼠标右键: 找到java 选择 Runable JAR file N ...

  10. vue中使用第三方插件animate.css实现动画效果

    vue中使用第三方插件animate.css实现动画效果1.首先先引入第三方类animated.css2.将你所需要动画的标签用包裹起来3.在transition元素中添加enter-active-c ...