【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|.
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的更多相关文章
- 【LeetCode】853. Car Fleet 解题报告(Python)
[LeetCode]853. Car Fleet 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- php开启短标签与<?xml version="1.0" encoding="UTF-8"?>冲突
前两天写了个生成mapsite的源码,想提交到百度和谷歌,在本地测试一点问题都没有,但是在服务器上就显示500错误 最后废了九牛二虎之力,终于可以设置iis直接显示出错信息. 也搞懂了问题所在,默认服 ...
- 图解 HTTP 笔记(八)——常见 Web 攻击技术
本章主要讲解 HTTP 通信过程中的一些常见 Web 攻击技术 一.跨站脚本攻击 跨站脚本攻击(Cross-Site Scripting, XSS)是指通过存在安全漏洞的 Web 网站注册用户的浏览器 ...
- 使用redisTemplate存储数据,出现\xAC\xED\x00\x05t\x00
本文开发环境:SpringBoot+RedisTemplate 代码: /** * 缓存Map * * @param key * @param dataMap * @return */ @Overri ...
- 按下F2编辑dxDBTreeView的节点
在TdxDBTreeView控件的OnKeyDown事件中写入if Key = VK_F2 thenbegin if DBTreeMain.DBSelected = nil then Exit ...
- LODOP直接导出图片不弹框
之前有博文测试了导出图片的图片长度关系,是直接弹窗的选择保存路径的方式:Lodop导出图片,导出单页内容的图片最近测试下不弹窗保存图片是否可以,样例是保存的excel,测试了下图片,图片也是可以的,该 ...
- Flutter 的异步机制Future
Dart是一个单线程语言,可以理解成物理线路中的串联,当其遇到有延迟的运算(比如IO操作.延时执行)时,线程中按顺序执行的运算就会阻塞,用户就会感觉到卡顿,于是通常用异步处理来解决这个问题. Dart ...
- python列表删除--remove(),del,pop()
remove()参数为列表元素,若在列表中,删除,不在则报错 如: pop()不带参数时默认删除列表的末尾元素并返回该元素,带参数时该参数为列表元素的下标值 不带参数: 以下标为参数: del 后面可 ...
- linux下使用openssl和md5sum加密文件或者字符串
#openssl //在终端中输入openssl后回车. OpenSSL> md5 //输入md5后回车 123456 //接着输入123456,不要输入回车.然后按3次ctr ...
- shell窗体实现代码雨(解闷用)
命令过程 277 2019-11-07 17:14:39 wget https://sourceforge.net/projects/cmatrix/files/cmatrix/1.2a/cmatri ...
- numpy中np.random.seed()的详细用法
在进行机器学习和深度学习中,我们会经常用到np.random.seed(),利用随机数种子,使得每次生成的随机数相同. numpy.randn.randn(d0,d1,...,dn) randn函数根 ...