[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 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/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵的更多相关文章
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- [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 ...
- 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. ...
- 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 ...
- leetcode Largest Rectangle in Histogram 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...
随机推荐
- 安卓v7支持包下的ListView替代品————RecyclerView
RecyclerView这个控件也出来很久了,相信大家也学习的差不多了,如果还没学习的,或许我可以带领大家体验一把这个艺术般的控件. 项目已经同步至github:https://github.com/ ...
- 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作
一.reindex() 方法:重新索引 针对 Series 重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...
- 深入理解脚本化CSS系列第二篇——查询计算样式
× 目录 [1]getComputedStyle [2]注意事项 [3]currentStyle[4]IE 前面的话 元素的渲染结果是多个CSS样式博弈后的最终结果,这也是CSS中的C(cascade ...
- Java进击C#——语法之线程同步
上一章我们讲到关于C#线程方向的应用.但是笔者并没有讲到多线程中的另一个知识点--同步.多线程的应用开发都有可能发生脏数据.同步的功能或多或少都会用到.本章就要来讲一下关于线程同步的问题.根据笔者这几 ...
- 【转】Asp.net MVC定义短网址
在MVC的逻辑代码里,Controller和Action是必须的,但是在网址里,并不需要完全体现Controller和Action.比如我们经常希望看到http://localhost/About而不 ...
- WebAPI接收JSON参数注意事项
运行环境:ASP.NET 4.5.2. 当我们向GlobalConfiguration.Configuration.MessageHandlers添加一个DelegatingHandler派生类后,很 ...
- 设计模式(十一)外观模式(Facade Pattern)
一.引言 在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化,然而为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ...
- ABP总体介绍
ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用程序的新起点, ...
- VMware安装CentOS时,无法以图形界面安装解决办法
有的同学问: 用虚拟机软件(vmware.VirtualBox)安装CentOS系统时, 安装过程中没有中文,也没有出现图形界面,都是以命令行方式去安装, 有时候又会出现图形界面,不知道哪里配置的问题 ...
- Canvas——使用定时器模拟动态加载动画!
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...