[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 ...
随机推荐
- BZOJ 1419: Red is good
Sol 期望DP. \(f[i][j]\) 表示剩下 \(i\) 张红牌, \(j\) 张黑牌的期望. 有转移方程. \(f[i][j]=0,i=0\) 没有红色牌了,最优方案就是不再翻了. \(f[ ...
- 获取客户端IP
function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...
- 【工具】【版本控制】TortoiseSVN过滤文件与文件夹
这些网上搜一大把,就直接截图过来了.
- ext grid 子表格
Ext.define('app.view.main.biz.customer.receipt.followup.FollowUpActionPanel', { extend: 'Ext.grid.Pa ...
- espcms自定义表单邮件字段
/include/inc_replace_mailtemplates.php中增加一行就可以了. 如:$replacemail['mailform'][] = array(name => '职位 ...
- ubuntu下查找某个文件的路径
参考:ubuntu下如何查找某个文件的路径 http://blog.csdn.net/lieyanhaipo/article/details/17055667 在Ubuntu有时候会遇到记得 ...
- iOS CLLocationManager 定位
今天写个定位,本来很简单,但是在填写plist时,通过系统提示,只能看到NSLocationUsageDescription项目,根本不提示 (1)NSLocationAlwaysUsageDescr ...
- iOS 在UITableViewCell中加入自定义view时view的frame设定注意
由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...
- java视频转码博客
一下为找到的资料地址 http://lichen.blog.51cto.com/697816/162124 http://www.cnblogs.com/live365wang/archive/201 ...
- [C#]Datatable和json互相转换操作
#region DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...