边工作边刷题: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 ...
随机推荐
- HTML页面导航栏页脚不动,变换中间部分
代码段一: <script> $(document).ready(function() { $("#teachingObjectives").click(functio ...
- YII 的基本CURL操作
Order::model()->updateAll(array('merchant_id'=>'bbb'), "id in ('140868169311','1408690584 ...
- PHP程序z中xdebug工具简要使用方法
PHP程序的debug PHP程序的debug,无论是cli方式还是web方式,都需要使用第三方的debug工具.PHP5.6之前,本身自带的debug功能,仅限于日志输出. 推荐使用免费xdebug ...
- linux多线程-互斥&条件变量与同步
多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...
- TreeView递归绑定无限分类数据
TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...
- SharePoint 错误集 3
1. workflow 流程走不下去,报 workflow fails to run 的错误 请确保下面二个service要么都start,要么都stop: Microsoft SharePoint ...
- 解析plist文件(字典里包着数组,数组中又包含字典)
#import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...
- .NET下金额大小写转换
说明:金额转换可以转换50位的数值,单位从分到级.对于中间部分是否显示零,可以根据修改TranslateJInen()函数修改.中间数值为零的去掉不显示 public string GetChCapi ...
- Mac上的软件使用介绍
目录大纲: Drop to GIF Parallels Desktop 1.Drop to GIF 功能:将视频文件可以生产动态图gif文件 网址在github上:https://github.com ...
- Android Design Support Library——TabLayout
TabLayout——选项卡布局,通过选项卡的方式切换view并不是material design中才有的新概念,选项卡既可以固定,也可以滚动显示效果如下: 通过addTab方法可以实现选项卡的动态添 ...