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)

https://repl.it/Cego/1

错误点:

  • 最后结果不是/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的更多相关文章

  1. 边工作边刷题:70天一遍leetcode: day 86

    Word Pattern II 要点: 注意与I的差异,其实题不难,看到这种迷乱的,首先要想到backtrack 1:1 mapping两个条件:p in and str in, or p not i ...

  2. 边工作边刷题:70天一遍leetcode: day 89

    Word Break I/II 现在看都是小case题了,一遍过了.注意这题不是np complete,dp解的time complexity可以是O(n^2) or O(nm) (取决于inner ...

  3. 边工作边刷题:70天一遍leetcode: day 77

    Paint House I/II 要点:这题要区分房子编号i和颜色编号k:目标是某个颜色,所以min的list是上一个房子编号中所有其他颜色+当前颜色的cost https://repl.it/Chw ...

  4. 边工作边刷题:70天一遍leetcode: day 78

    Graph Valid Tree 要点:本身题不难,关键是这题涉及几道关联题目,要清楚之间的差别和关联才能解类似题:isTree就比isCycle多了检查连通性,所以这一系列题从结构上分以下三部分 g ...

  5. 边工作边刷题:70天一遍leetcode: day 85-3

    Zigzag Iterator 要点: 实际不是zigzag而是纵向访问 这题可以扩展到k个list,也可以扩展到只给iterator而不给list.结构上没什么区别,iterator的hasNext ...

  6. 边工作边刷题:70天一遍leetcode: day 101

    dp/recursion的方式和是不是game无关,和game本身的规则有关:flip game不累加值,只需要一个boolean就可以.coin in a line II是从一个方向上选取,所以1d ...

  7. 边工作边刷题:70天一遍leetcode: day 1

    (今日完成:Two Sum, Add Two Numbers, Longest Substring Without Repeating Characters, Median of Two Sorted ...

  8. 边工作边刷题:70天一遍leetcode: day 70

    Design Phone Directory 要点:坑爹的一题,扩展的话类似LRU,但是本题的accept解直接一个set搞定 https://repl.it/Cu0j # Design a Phon ...

  9. 边工作边刷题:70天一遍leetcode: day 71-3

    Two Sum I/II/III 要点:都是简单题,III就要注意如果value-num==num的情况,所以要count,并且count>1 https://repl.it/CrZG 错误点: ...

  10. 边工作边刷题:70天一遍leetcode: day 71-2

    One Edit Distance 要点:有两种解法要考虑:已知长度和未知长度(比如只给个iterator) 已知长度:最好不要用if/else在最外面分情况,而是loop在外,用err记录misma ...

随机推荐

  1. 平衡二叉树---Shaolin

    Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temp ...

  2. C# on Visual Studio Code

    installation Download .NET Core SDK installer and install it. https://www.microsoft.com/net/download ...

  3. 基于java的socket编程

    #开头的废话#学习java已经半个月了,原本在抠教材里面的字眼时,觉得教材好厚,要看完不知道要到猴年马月去了.突然在网上看到一个教程,里面老师说学编程语言书不用太细看,看个大概,知道里面讲些什么就好, ...

  4. Python for循环内部实现的一个sample

    #!/usr/bin/env python # -*- coding: utf-8 -*- it = iter([1,2,3,4,5]) while True: try: x = next(it) p ...

  5. js的alert和confirm美化

    --前言-- window对象的alert和confirm标准方法在不同浏览器的显示效果不太相同,有个相同点是都不是很美观.我们的想法是使用js和css分别仿照它们,提供另一套函数,使在不同浏览器的有 ...

  6. javascript宿主对象之window.screen、window.close()/open()、window.moveTo、window.resizeTo

    window.screen属性所提供的是浏览器以外的信息.这里只简单的概述一下: screen.availWidth - 可用的屏幕宽度 (除去操作系统菜单) screen.availHeight - ...

  7. JavaScript的作用域和闭包

    首发于:https://mingjiezhang.github.io/ 闭包和作用域有着千丝万缕的联系. js的作用域 具体的作用域我就不展开叙述了.其中很重要的两点就是:js的作用域链机制和函数词法 ...

  8. UIWebView的基本用法

    一.UIWebView的基础使用 1.创建UIWebView: CGRect bouds = [[UIScreen manScreen]applicationFrame]; UIWebView* we ...

  9. linux NFS服务器安装与配置 思路

    一,nfs服务优缺点 NFS 是Network File System的缩写,即网络文件系统,可以让不同的客户端挂载使用同一个目录,作为共享存储使用,这样可以保证不同的节点客户端数据一致性,在集群架构 ...

  10. python基本数据结构-集合-集合运算