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的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. LeetCode: Maximal Rectangle 解题报告

    Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...

  3. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  4. [leetcode]Maximal Rectangle @ Python

    原题地址:https://oj.leetcode.com/problems/maximal-rectangle/ 题意:Given a 2D binary matrix filled with 0's ...

  5. [LeetCode] Maximal Rectangle(good)

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

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

  7. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  8. leetcode面试准备: Maximal Rectangle

    leetcode面试准备: Maximal Rectangle 1 题目 Given a 2D binary matrix filled with 0's and 1's, find the larg ...

  9. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

随机推荐

  1. .oi 小游戏

    http://agar.io/ http://diep.io/ http://slither.io/ http://splix.io/ http://wilds.io/ http://kingz.io ...

  2. BZOJ 4596: [Shoi2016]黑暗前的幻想乡

    Sol 容斥原理+Matrix-Tree定理.容斥跟小星星那道题是一样的,然后...直接Matrix-Tree定理就可以了... 复杂度\(O(2^{n-1}n^3)\) PS:调了好久啊QAQ 明明 ...

  3. OI总结(垃圾排版就忽略了吧)

    学OI一年了,到现在联赛所需要的知识已经基本学完了.现在,有必要回过头来,总结总结自己一年来学到的知识以及得到的经验教训. 基础 语言基础 C++的语言基础啥的就略了吧. 算法复杂度分析 O:复杂度的 ...

  4. 关闭CENTOS不必要的默认服务

    CentOS关闭服务的方法: 图形界面,运行ntsysv chkconfig –level 2345 服务名称 off 服務名稱 建議 說明 acpid 停用 Advanced Configurati ...

  5. 用extern定义全局变量

    1.extern的作用 extern有两个作用,第一个,当它与"C"一起连用时,如: extern "C" void fun(int a, int b); 则告 ...

  6. 如何在maven中添加本地jar包

    mvn install:install-file -DgroupId=mytest-DartifactId=test-Dversion=1.1 -Dpackaging=jar -Dfile=d:\te ...

  7. 【转载】通过JDBC对MySQL数据库的增删改查

    通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...

  8. iOS NSObject 的 isa 属性的类型 Class

    以前对NSObject的isa属性也知道点,但是了解不深,今天看了这篇博文,感觉很好,总结一下: http://chun.tips/blog/2014/11/05/bao-gen-wen-di-obj ...

  9. socket端口重复占用问题

    1.一个服务端进程在主动释放端口后(调用close)端口状态为TIME_WAIT,这时再去监听同样的端口,不论是否设置SO_REUSEADDR,都能监听成功,也能接收到客户端的连接,但是无法收到数据. ...

  10. Python 路谱

    20160214--->20160824 一口气爬过高山 Happy Valentine’s Day https://docs.python.org/2.7/library/index.html ...