边工作边刷题:70天一遍leetcode: day 86-2
Best Meeting Point
要点:
- 题本身不难理解,manhattan distance。follow up就变成weighted了(因为一个地方可以有多个住户)
- 注意input是grid的形式,一种方法是2d iterate,然后用两个数组分别存x,y,只需要对column sort,row是按顺序的iterate的。最后直接取中
- 这题之所以是Hard,因为有另一种方法:不用sort,不用找median,类似于nested weighted sum II,只不过变成双向而非单向
- 假设到某一点i和j分别对应左右的某点(注意,和另一边无关),left表示所有i左边的人,right表示所有j右边的人。如果i右移,j左移,每移动一步所有当前left,right的距离d都会增加1。显然,在每一步选最小的增加距离最合算(到目前为止,不同层总的增加数是不同的)。而如果当前i/j上有人,人口也会增加(即left/right增加)。
- 为什么是i<j?当i==j的时候,这个位置的新增人(either left or right)距离都是0,所以不需要计入距离。推广到一般的过程,所有新增人口,都不会对当前轮的d有影响。所以要d+=left/right是上一轮的
- 另外,也利用了each row/col的sum,i.e.,把行列浓缩成一个值来降维。
- 显然,这个方法也可以处理weighted的情况。所以time complexity: O(mn)
- 最后结果不是/2
# 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?
# Hide Company Tags Twitter
# Hide Tags Math Sort
# Hide Similar Problems (H) Shortest Distance from All Buildings
class Solution(object):
def minTotalDistance(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
row_num = [sum(row) for row in grid]
col_num = [sum(col) for col in zip(*grid)]
def minDist(sum_list):
l, r = -1, len(sum_list)
left,right=0,0
d=0
while l<r:
if left<right:
d+=left
l+=1
left+=sum_list[l]
else:
d+=right
r-=1
right+=sum_list[r]
return d
return minDist(row_num)+minDist(col_num)
sol = Solution()
assert sol.minTotalDistance([[1,0,0,0,1],[0,0,0,0,0],[0,0,1,0,0]])==6
边工作边刷题:70天一遍leetcode: day 86-2的更多相关文章
- 边工作边刷题:70天一遍leetcode: day 86
Word Pattern II 要点: 注意与I的差异,其实题不难,看到这种迷乱的,首先要想到backtrack 1:1 mapping两个条件:p in and str in, or p not i ...
- 边工作边刷题:70天一遍leetcode: day 89
Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...
- 边工作边刷题:70天一遍leetcode: day 77
Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...
- 边工作边刷题:70天一遍leetcode: day 78
Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...
- 边工作边刷题:70天一遍leetcode: day 85-3
Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...
- 边工作边刷题:70天一遍leetcode: day 101
dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...
- 边工作边刷题:70天一遍leetcode: day 1
(今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...
- 边工作边刷题:70天一遍leetcode: day 70
Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...
- 边工作边刷题:70天一遍leetcode: day 71-3
Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...
- 边工作边刷题:70天一遍leetcode: day 71-2
One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...
随机推荐
- jQuery获取Select选择的Text和 Value(转)用时比较方便寻找
---恢复内容开始--- jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code. ...
- 以Web Host的方式来寄宿Web API
一.新建一个Common的类库项目并新建一个测试用的Contact实体类 namespace Common { public class Contact { public string Id { ge ...
- 我所了解的WEB开发(2) - PS切片
PS对于WEB设计和前端开发来说都是不可或缺的工具,基本的用途是用来处理网站的LOGO.Banner 以及按钮图标来着,但是一旦遇上要把整个PSD文件转成网页就让人非常头痛了,可能还不太专业.后来在公 ...
- ServiceLocator是反模式
关于ServiceLocator模式 http://www.cnblogs.com/hwade/archive/2011/01/30/CommonServiceLocator.html 为什么是Ant ...
- Angular框架
Angular 框架 Angular介绍 库和框架的区别 jQuery:库 库一般都是封装了一些常用的方法 自己手动去调用这些方法,来完成我们的功能 code $('#txt').val('我是小明' ...
- Vue表单
gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson11 一 vue表单 实在是太简单了,直接来个例子 <!DOC ...
- iOS8以后 UISearchController的用法
查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...
- 第八章 了解tempdb数据库
1.一个sqlserver数据库实例上只能有一个tempdb数据库,这个实例上所有的用户都共享这个数据库.2.tempdb数据库在每次sqlserver重启后都会重新创建,所以数据会丢失.3.因为te ...
- Spring AOP 深入剖析
AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充.使用AOP技术,可以将一些系统性相关的编程工作,独立提取出来,独立实现,然后通过切面切入进系统.从而避免了在业务逻 ...
- A Popup Progress Window
一个包含bar和取消而且不需要资源弹出窗口 1.构造函数 CProgressWnd(); CProgressWnd(CWnd* pParent, LPCTSTR strTitle, BOOL bSmo ...