[LeetCode] Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
在做Largest Rectangle in Histogram的时候有人说可以用在这题,看了一下还真是,以每行为x轴,每列往上累计的连续的1当成高度,就可以完全使用一样的方法了。
int largestArea(vector<int>height){
stack<int> s;
int maxArea = ;
int i = ;
height.push_back();
int len = height.size();
while (i < len){
if (s.empty() || height[s.top()] < height[i]){
s.push(i++);
}else{
int t = s.top();
s.pop();
maxArea = max(maxArea, height[t] * (s.empty()? i : (i-s.top()-)));
}
}
return maxArea;
}
int maximalRectangle(vector<vector<char>> &matrix){
vector<int> height;
int maxRect=;
for (int row=; row<matrix.size(); row++){
for (int col=; col<matrix[row].size(); col++){
if (matrix[row][col] == ''){
height.push_back();
}
else{
int c=;
for (int i=row; i>-; i--){
if (matrix[i][col] != ''){
c++;
}else {
break;
}
}
height.push_back(c);
}
}
for (int i=;i<height.size(); i++){
cout << height[i] << " ";
}
cout << endl;
maxRect = max(maxRect, largestArea(height));
height.clear();
}
return maxRect;
}
int maximalRectangle2(vector<vector<char>> &matrix){
int maxRect = ;
if (matrix.size() < ) return ;
vector<int>height(matrix[].size(), );
for (int i=; i<matrix.size(); i++){
for (int j=; j<matrix[i].size(); j++){
if (matrix[i][j] == ''){
height[j] += ;
}else{
height[j] = ;
}
}
maxRect = max(maxRect, largestArea(height));
}
return maxRect;
}
第一个maximalRectangle函数我用了很蠢的遍历方法来获得每行的height,这样活生生的把原本O(n^2)搞成了O(n^3)。。。 最后看了别人博客,说height是可以通过前一行的值算出了的(有点类似DP的思想...如果这也算的话),豁然开朗,才写出了
maximalRectangle2这个真正的O(n^2)方法。
[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(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 ...
随机推荐
- 5.8---像素设定(CC150)
注意:仔细注意位置. public static int[] renderPixel(int[] screen, int x, int y){ int[] ans = new int[screen.l ...
- django xadmin 外键
style_fields = {'db栏位名称': "fk-ajax"} 实体关系: Account (*)-->(1) user 表单控件: 下拉框 美化用了selecti ...
- backbone & django csrf_token的问题
由于这个加入了token的验证,因此在backbone调用Model/Collection的save时会失败,错误403.(这里不讨论劫持重发的问题) 解决方案是:修改xmlHttpRequest的h ...
- TCP三次握手原理详解
TCP/IP协议不是TCP和IP这两个协议的合称,而是指因特网整个TCP/IP协议族. 从协议分层模型方面来讲,TCP/IP由四个层次组成:网络接口层.网络层.传输层.应用层. TCP协议:即传输控制 ...
- Insert Delete GetRandom O(1) I & II
Design a data structure that supports all following operations in O(1) time. insert(val): Inserts an ...
- MQTT V3.1----publish解读
客户端/服务器的发布消息行为,与PUBLISH相关的消息类型: PUBLISH 客户端发布消息经由服务器分发到所有对应的订阅者那里.一个订阅者可以订阅若干个主题(Topic name),但一个PUBL ...
- 华为 MATE7 调试 LOCAT 日志不输出问题
[转]华为 MATE7 调试 LOCAT 日志不输出问题 http://www.cnblogs.com/glaivelee/p/4593221.html 用手机进行调试,在电脑上不显示logcat信息 ...
- Unity3d《Shader篇》变胖
变胖前 变胖后 //Shader Shader "Custom/NormalExt" { Properties { _MainTex("Base (RGB)", ...
- PHP接入umeditor(百度富文本编辑器)
2015年6月28日 23:08:49 星期日 效果: 开搞;) 首先: 百度官网上下载 umeditor 简版的富文本编辑器(这里) 然后: 解压放到自己的项目中, 配置服务器, 保证能在浏览器端加 ...
- problem-record-mysql
#!/bin/bash # # Update_Problem - updates problem record in database ################################ ...