LeetCode——maximal-rectangle
Question
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
Solution
这个题目可以借鉴LeetCode——largest-rectangle-in-histogram 的思路,求最大矩阵的面积,所以我们只需要按行把给定’0’,‘1’矩阵,转换成对应的高度即可,然后计算最大矩阵面积。
Code
时间复杂度O(n^2).
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
int row = matrix.size();
if (row <= 0)
return 0;
int col = matrix[0].size();
vector<vector<int>> heights;
for (int i = 0; i < row; i++) {
vector<int> tmp;
for (int j = 0; j < col; j++) {
if (matrix[i][j] != '0') {
// 累加高度
if (i - 1 >= 0 && matrix[i - 1][j] != '0')
matrix[i][j] += (matrix[i - 1][j] - '0');
tmp.push_back(matrix[i][j] - '0');
} else {
// 被‘0’断开了,就需要计算一次
if (tmp.size() > 0) {
heights.push_back(tmp);
tmp.clear();
}
}
}
if (tmp.size() > 0)
heights.push_back(tmp);
}
int res = 0;
for (int i = 0; i < heights.size(); i++) {
res = max(res, calc(heights[i]));
}
return res;
}
// 给定高度,求最大矩阵面积,时间复杂度O(n)
int calc(vector<int>& height) {
if (height.size() <= 0)
return 0;
stack<int> tb;
int n = height.size();
int res = 0;
for (int i = 0; i < n; i++) {
while (!tb.empty() && height[tb.top()] >= height[i]) {
int index = tb.top();
tb.pop();
if (tb.empty()) {
res = max(res, i * height[index]);
} else {
res = max(res, (i - tb.top() - 1) * height[index]);
}
}
tb.push(i);
}
while (!tb.empty()) {
int index = tb.top();
tb.pop();
if (tb.empty()) {
res = max(res, n * height[index]);
} else {
res = max(res, (n - tb.top() - 1) * height[index]);
}
}
return res;
}
};
LeetCode——maximal-rectangle的更多相关文章
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [leetcode]Maximal Rectangle @ Python
原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...
- [LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Maximal Rectangle(good)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- leetcode -- Maximal Rectangle TODO O(N)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- leetcode面试准备: Maximal Rectangle
leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...
- [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
随机推荐
- window下cmd的宽度调整
一直被cmd的宽度这么了好些年,刚才搜索了下,还真可以设置.标题栏右键属性,调整屏幕缓冲区宽度,只要足够长就不会换行了,然后调整窗口大小-宽度,不能超过屏幕缓冲区宽度,当小于屏幕缓冲区的时候,就会显示 ...
- python基础-第八篇-8.1初识Socket
socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...
- 1.1 - python基础语法 - 总结练习题
1.编译型与解释型语言的区别,哪些属于编译型,哪些属于解释型 编译型:c/c++/go 运行速度快,开发效率低,不可跨平台 解释型:python/java/php/ruby 运行速度低,开发效率高,可 ...
- Centos7编译4.7.2内核
由于想要编译kvm-kmod,编译的时候提示内核版本太低,所以就不得不升级下linux内核,目前最新版内核是4.7.2,编译内核并不是一件很难的事,但是这么久没编译过,还是有必要记录下这一过程. 首先 ...
- mysql数据池设置
参考链接https://www.cnblogs.com/KKSoft/p/8040374.html python的数据库连接池包:DBUtils DBUtils提供两种外部接口: Persistent ...
- mysql ERROR 1264 (22003): Out of range value for column 'x' at row 1 错误
mysql> insert into t1 values (-129), (-128), (127),(128);ERROR 1264 (22003): Out of range value f ...
- html5游戏开发--"动静"结合(二)-用地图块拼成大地图 & 初探lufylegend
一.前言 本次教程将向大家讲解如何用HTML5将小地图块拼成大地图,以及如何用现有的高级html5游戏开发库件lufylegend.js开发游戏. 首先让我们来了解了解如何用html5实现动画,毕竟“ ...
- POJ1664:放苹果(线性dp)
题目: http://poj.org/problem?id=1664 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1 ...
- append和extend区别
append append方法用于在列表末尾添加新的对象 它是把添加的对象当成一个整体追加到末尾 a=[1,23,436] b=[] b.append(a) print(b)""& ...
- XDU 1037 倍流畅序列(字符串匹配+贪心)
题目描述 对于一个0,1串s, 从左端开始读取它的0获得序列s0,从右端开始读取它的1获得s1,如果s0与s1同构,则称s为倍流畅序列.例如:011001是一个倍流畅序列, 因为:s0 = 0__00 ...