Smallest Rectangle Enclosing Black Pixels

要点:记题:这题有两个限制条件:所有black pixel是连通的(所以可以用binary search)以及给了一个black pixel。

错误理解:给定black pixel所在行/列的top/down/left/right是不正确的。因为这题为2d,二分的条件是一整行/列上是否有black pixel,也就是把整个行/列作为一个元素,而对应的列/行作为搜索array。给定的点只是作为low/high的初始条件

  • 二分是bounded binary search,类似First Bad Version,什么意思?就是二分搜索只有两部分,符合条件的是high end(因为>high的部分都符合)

    • top:对行二分(0和x之间),某一行有black pixel是high
    • bottom:同样对行二分(x到m),某一列没有black pixel是high
    • left:类似top:列二分(0到y之间),某一行有black pixel是high
    • right:类似bottom,对列二分(y到n),某一行没有black pixel是high
  • 为什么能统一code(just pass in check)?left/top搜索第一个1,right/bottom搜索第一个0,目标都是high end

https://repl.it/Cdja/5

错误点


- bounded binary search如果high为highest index+1,可以自动handle边界条件:如果找left,那么如果left不存在(比如这题全是0)。那么最终返回high。这是因为low==high是不会处理的,最后的mid是low==high的mid
- y轴如何写lambda?loop row,然后每个row[y] list comprehension就得到list了。也可以用any()/all() # An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels. # For example, given the following image: # [
# "0010",
# "0110",
# "0100"
# ]
# and x = 0, y = 2,
# Return 6. class Solution(object):
def minArea(self, image, x, y):
"""
:type image: List[List[str]]
:type x: int
:type y: int
:rtype: int
"""
def binary(image, low, high, check):
mid = low + (high-low)/2
while low<high:
if check(mid):
high = mid
else:
low = mid+1
mid = low + (high-low)/2
return mid top = binary(image, 0, x, lambda x: '1' in image[x])
down = binary(image, x+1, len(image), lambda x: '1' not in image[x])
left = binary(image, 0, y, lambda y: '1' in [row[y] for row in image])
right = binary(image, y+1, len(image[0]), lambda y: '1' not in [row[y] for row in image])
return (right-left)*(down-top) sol = Solution()
assert sol.minArea([['0','0','1','0'], ['0','1','1','0'], ['0','1','0','0']], 0,2)==6, "must be 6"

边工作边刷题:70天一遍leetcode: day 89-1的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Longest Substring with At Most K Distinct Characters 要点:要搞清楚At Most Two Distinct和Longest Substring W ...

随机推荐

  1. Linux修改命令提示符(关于环境参量PS1)

    关乎环境参量的四个文件/etc/profile  /etc/bashrc ~/.bashrc  ~/.bash_profile $$$:/etc/profile:此文件为系统的每个用户设置环境信息,当 ...

  2. Erlang 内存泄漏分析

    随着项目越来越依赖Erlang,碰到的问题也随之增加.前段时间线上系统碰到内存高消耗问题,记录一下troubleshooting的分析过程.线上系统用的是Erlang R16B02版本. 问题描述 有 ...

  3. 【Android】开源项目UI控件分类汇总之Dialog

    接前文ProgressBar:Android开发的宝库越来越多,我开发中有需要的组件,主要参考Trinea的大作Android开源项目分类汇总(包含了后面的绝大多数).CSDN上直接拿来用!最火的An ...

  4. Exchange 2013 、Lync 2013、SharePoint 2013 二

    上一篇简单介绍了安装过程,本篇主要集成 上一篇文章有关于头像的显示问题,engineer  给出了一个连接,介绍了Exchange和Lync的集成过程,根据介绍都配制了一遍. 一.Exchange 和 ...

  5. Virtual DOM 算法

    前端 virtual-dom react.js javascript 目录: 1 前言 2 对前端应用状态管理思考 3 Virtual DOM 算法 4 算法实现 4.1 步骤一:用JS对象模拟DOM ...

  6. 【原/转】iOS中非常强大的过滤器:NSPredicate

    在APPLE的官方Demo:UICatalog中实现UISearchBar模糊搜索功能是这么做的: - (void)viewDidLoad { [super viewDidLoad]; self.al ...

  7. iOS 核心动画

    核心动画(Core Animation) : •CoreAnimation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.fr ...

  8. OC-分类

    1.不能再分类里面添加属性, 只能添加方法. 2.如果在分类里面使用@property,那么他只生成sette,getter的声明而没有实现. 3.如在在分类中写了与本类同名的方法,优先调用分类里面的 ...

  9. iOS之 PJSIP静态库编译(一)

    首先放上pjsip官方网站http://www.pjsip.org/download.htm 下载的时候注意while the .bz2 has LF line-ends and is for Uni ...

  10. Objective-C之@class的使用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...