原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/

题目:

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 = 0y = 2,

Return 6.

题解:

利用binary search来找四个边界。

举例找左边第一个'1', 从横向的0到y, 取mid, 若是mid对应的这一列若是包含'1', 说明左边第一个'1'在mid左侧,在mid左侧继续查找.

找右侧第一个'0', 从很像的y+1到image[0].length, 取mid, 若是mid对应的这一列包含'1', 说明右边第一个'0'在mid右侧,在mid右侧继续查找.

右侧第一个'0'的index 减掉 左侧第一个'1'的index 就是横向包含'1'的长度.

上下也是同理.

Time Complexity: O(m*logn + n*logm). Space: O(1).

AC Java:

 public class Solution {
public int minArea(char[][] image, int x, int y) {
if(image == null || image.length == 0 || image[0].length == 0){
return 0;
}
int m = image.length;
int n = image[0].length; if(x<0 || x>=m || y<0 || y>=n){
return 0;
} int left = binarySearch(image, 0, y, 0, m, true, true); //左边第一个 '1'
int right = binarySearch(image, y+1, n, 0, m, true, false); //右边第一个 '0'
int up = binarySearch(image, 0, x, 0, n, false, true); // 上边第一个 '1'
int down = binarySearch(image, x+1, m, 0, n, false, false); //下边第一个 '0' return (right-left) * (down - up);
} private int binarySearch(char [][] image, int low, int high, int min, int max, boolean searchHor, boolean searchFirstOne){
while(low < high){
int mid = low + (high - low)/2; boolean existBlack = false;
for(int i = min; i<max; i++){
if((searchHor ? image[i][mid] : image[mid][i]) == '1'){
existBlack = true;
break;
}
} if(existBlack == searchFirstOne){
high = mid;
}else{
low = mid+1;
}
}
return low;
}
}

LeetCode Smallest Rectangle Enclosing Black Pixels的更多相关文章

  1. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  2. 【leetcode】302.Smallest Rectangle Enclosing Black Pixels

    原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...

  3. 302. Smallest Rectangle Enclosing Black Pixels

    题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...

  4. [Swift]LeetCode302. 包含黑色像素的最小矩形 $ Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  5. Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  6. [Locked] Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. Smallest Rectangle Enclosing Black Pixels 解答

    Question An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. ...

  8. LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  9. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

随机推荐

  1. ccc 函数中写函数

    attackOnTarget: function (atkDir, targetPos) { var self = this; let deg = cc.radiansToDegrees(cc.pAn ...

  2. 21045308刘昊阳 《Java程序设计》第九周学习总结

    21045308刘昊阳 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立运行的应用程序 撰 ...

  3. ACM 喷水装置(一)

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以 ...

  4. [WP8.1UI控件编程]Windows Phone VirtualizingStackPanel、ItemsStackPanel和ItemsWrapGrid虚拟化排列布局控件

    11.2.2 VirtualizingStackPanel.ItemsStackPanel和ItemsWrapGrid虚拟化排列布局控件 VirtualizingStackPanel.ItemsSta ...

  5. win8.1上wamp环境中利用apache自带ab压力测试工具使用超简单讲解

    2015.10.4apache自带ab压力测试工具使用:本地环境:win8.1 wampserver2.5 -Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b 可以参考一 ...

  6. Linux文件锁flock

    Linux文件锁flock 在多个进程同时操作同一份文件的过程中,很容易导致文件中的数据混乱,需要锁操作来保证数据的完整性,这里介绍的针对文件的锁,称之为“文件锁”-flock. flock,建议性锁 ...

  7. GX转账站点无法访问的问题

    1.先检查200路由器上的端口<8099,8098> 没有则加上 2.检查站点是否能在200服务器上打开 不能打开,重启站点

  8. mysql语句 索引操作

    创建索引:(help create index;) CREATE INDEX indexName ON tableName(Coll,Coll....); ALTER TABLE tableName ...

  9. mysqldump备份详解

    -A  备份所有-B  恢复时会自动创建库  (同时支持导出多个库  -B  db01 db02) -d  导出表结构 #库中有多个表导出时导出没加 –B参数,则要先导入结构,如果表结构没有备份,那就 ...

  10. PLSQL Developer注册码

    Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769password:xs374ca