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.

Example:

Input:
[
"0010",
"0110",
"0100"
]
and x = 0, y = 2 Output: 6

这道题给我们一个二维矩阵,表示一个图片的数据,其中1代表黑像素,0代表白像素,现在让找出一个最小的矩阵可以包括所有的黑像素,还给了一个黑像素的坐标,先来看 Brute Force 的方法,这种方法的效率不高,遍历了整个数组,如果遇到了1,就更新矩形,参见代码如下:

解法一:

// Brute force
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
int left = y, right = y, up = x, down = x;
for (int i = ; i < image.size(); ++i) {
for (int j = ; j < image[i].size(); ++j) {
if (image[i][j] == '') {
left = min(left, j);
right = max(right, j);
up = min(up, i);
down = max(down, i);
}
}
}
return (right - left + ) * (down - up + );
}
};

下面这种解法是解法一的递归写法,本质上来说跟上面的解法没有啥区别,也没有任何的优化,所以仍然可以认为是暴力搜索法,参见代码如下:

解法二:

// DFS
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
int left = y, right = y, up = x, down = x;
dfs(image, x, y, left, right, up, down);
return (right - left + ) * (down - up + );
}
void dfs(vector<vector<char>> &image, int x, int y, int &left, int &right, int &up, int &down) {
if (x < || x >= image.size() || y < || y >= image[].size() || image[x][y] != '') return;
left = min(left, y);
right = max(right, y);
up = min(up, x);
down = max(down, x);
image[x][y] = '';
dfs(image, x + , y, left, right, up, down);
dfs(image, x - , y, left, right, up, down);
dfs(image, x, y + , left, right, up, down);
dfs(image, x, y - , left, right, up, down);
}
};

我们再来看一种优化了时间复杂度的解法,这是一种二分搜索法,以给定的一个黑像素 (x, y) 为中心,分别用二分法快速找到整个黑色区域的上下左右的临界点,然后直接算出面积。首先来看上边界怎么找,既然是以 (x, y) 为中心,而且上边界又是某个行数,那么其范围肯定在 [0, x] 之间,能成为上边界的条件是该行中至少有一个点是1,那么其列数的范围就在 [0, n] 之间,在进行二分搜索的时候,先根据i, j算出中间行 mid,然后列数从0开始遍历,直到找到为1的点,或者越界位置,然后判断列数是否越界,越界的话,说明当前行没有1,此时更新i为 mid+1,如果找到了1,那么更新j为 mid。找下边界也是同样的道理,但是跟上边界稍微又些不同的地方是,如果当前行找到了1,应该再往下找,那么i应该更新为 mid+1;如果没找到,就应该往上找,靠近 (x, y) 点;所以两种情况只是在二分法更新范围的地方正好相反,所以可以用一个 bool 型的变量 opt 来决定还如何更新行数。

下面来看如何确定左边界和右边界,其实跟确定上下边界大同小异。左边界是列数,若以 (x, y) 点为中心,那么其范围便是 [0, y],因为之前已经确定了上下边界 up 和 down 了,所以左边界点的行数范围就是 [up, down],同理,当通过i, j求出了中间列 mid 时,就要遍历该列,找到为1的点,所以此时是用 image[k][mid],而在找上下边界时,用的是 image[mid][k],还是顺序不一样,可以用另外一个 bool 型变量h来控制,h表示 horizontal,就是水平遍历的意思。这样通过两个 bool 型变量就可以用一个函数来涵盖四种情况的二分搜索,是不是很叼?下面更新i或j的时候参考上下边界的分析,应该不难理解,参见代码如下:

解法三:

// Binary Search
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
int m = image.size(), n = image[].size();
int up = binary_search(image, true, , x, , n, true);
int down = binary_search(image, true, x + , m, , n, false);
int left = binary_search(image, false, , y, up, down, true);
int right = binary_search(image, false, y + , n, up, down, false);
return (right - left) * (down - up);
}
int binary_search(vector<vector<char>> &image, bool h, int i, int j, int low, int high, bool opt) {
while (i < j) {
int k = low, mid = (i + j) / ;
while (k < high && (h ? image[mid][k] : image[k][mid]) == '') ++k;
if (k < high == opt) j = mid;
else i = mid + ;
}
return i;
}
};

参考资料:

https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/

https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/discuss/75128/1ms-Concise-Java-Binary-Search-(DFS-is-4ms)

https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/discuss/75127/C%2B%2BJavaPython-Binary-Search-solution-with-explanation

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵的更多相关文章

  1. LeetCode Smallest Rectangle Enclosing Black Pixels

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

  2. [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 ...

  3. 【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 ...

  4. 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 ...

  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 Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

随机推荐

  1. JavaScript : 零基础打造自己的类库

    写作不易,转载请注明出处,谢谢. 文章类别:Javascript基础(面向初学者) 前言 在之前的章节中,我们已经不依赖jQuery,单纯地用JavaScript封装了很多方法,这个时候,你一定会想, ...

  2. JS魔法堂:不完全国际化&本地化手册 之 实战篇

    前言  最近加入到新项目组负责前端技术预研和选型,其中涉及到一个熟悉又陌生的需求--国际化&本地化.熟悉的是之前的项目也玩过,陌生的是之前的实现仅仅停留在"有"的阶段而已. ...

  3. iOS关于模块化开发解决方案(纯干货)

    关于iOS模块化开发解决方案网上也有一些介绍,但真正落实在在具体的实例却很少看到,计划编写系统文章来介绍关于我对模块化解决方案的理解,里面会有包含到一些关于解耦.路由.封装.私有Pod管理等内容:并编 ...

  4. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  5. css全局格式化

    /*全局控制*/ body{margin:0;padding:0;font-size:14px;line-height:22px; height:auto; font-family:"微软雅 ...

  6. php的面向对象

    今天PO一段php的面向对象相关知识吧.面向对象的相关概念和理论知识是很抽象的,要结合现实中的事物来理解,这样有助于类比记忆.还有就是要多接触吧,量变引质变这个应该还是一个硬道理吧,有时候量够了的话, ...

  7. 每天一个设计模式-2 外观模式(Facade)

    每天一个设计模式-2  外观模式(Facade) 1.生活中的示例 客户想要购买一台电脑,一般有两种方法: 1.自己DIY,客户需要知道组成电脑的所有电子器件,并且需要熟悉那些配件,对客户要求较高. ...

  8. Spring的IOC和AOP之深剖

    今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...

  9. input文本框录入字母自动大写

    向文本框输入文字时,如何让小写字母自动变为大写呢?有一个简单有效的做法是用CSS. <input name="t1" type="text" style= ...

  10. Android 使用pull,sax解析xml

    pull解析xml文件 1.获得XmlpullParser类的引用 这里有两种方法 //解析器工厂 XmlPullParserFactory factory=XmlPullParserFactory. ...