作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html

题目链接:leetcode Maximal Rectangle 单调栈

该题目是  leetcode Largest Rectangle in Histogram 的二维版本,首先进行预处理,把一个n×m的矩阵化为n个直方图,然后在每个直方图中计算使用单调栈的方法计算面积最大的矩形,然后求得最大的矩形面积即可。

Ps:这题直接在网页里面敲完居然1A,不错。

代码如下:

 class Solution {
public:
int mr(vector<int> arr)
{
arr.push_back();
stack<pair<int, int> > st;//height index
int res = ;
for( int i = ; i < arr.size() ; i++ )
{
pair<int, int> tmp;
if( st.size() == || st.top().first <= arr[i] )
{
st.push(make_pair(arr[i], i));
}
else
{
while( st.size() > && st.top().first > arr[i] )
{
tmp = st.top();
st.pop();
res = max(res, tmp.first*(i-tmp.second));
}
st.push(make_pair(arr[i], tmp.second));
}
}
return res;
}
int maximalRectangle(vector<vector<char> > &matrix)
{
int res = ;
if( matrix.size() == ) return ;
vector<vector<int> > ma(matrix.size(), vector<int>(matrix[].size(), ));
for( int i = ; i < matrix[].size(); i++ )
{
if( matrix[][i] == '' ) ma[][i] = ;
}
for( int i = ; i < matrix.size() ; i++ )
{
for( int j = ; j < matrix[].size() ; j++ )
{
if( matrix[i][j] == '' )
{
ma[i][j] = ma[i-][j] + ;
}
}
}
for( int i = ; i < ma.size() ; i++ )
{
res = max(res, mr(ma[i]));
}
return res;
}
};

leetcode Maximal Rectangle 单调栈的更多相关文章

  1. LeetCode: Maximal Rectangle 解题报告

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

  2. poj 2559 Largest Rectangle(单调栈)

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549 ...

  3. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法

    Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...

  4. [每日一题2020.06.13]leetcode #739 #15 单调栈 双指针查找

    739 每日温度 ( 单调栈 ) 题目 : https://leetcode-cn.com/problems/daily-temperatures/ 题意 : 找到数组每一个元素之后第一个大于它的元素 ...

  5. [LeetCode] Maximal Rectangle 最大矩形

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

  6. [leetcode]Maximal Rectangle @ Python

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

  7. [LeetCode] Maximal Rectangle

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

  8. [LeetCode] Maximal Rectangle(good)

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

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

随机推荐

  1. 关于STM32下载问题的简单理解

    首先STM32分为两种下载方式1.ISP(IN-SYSTEM-PROGRAMMING在线编程)  2.JTAG 这里简单谈谈对ISP下载的理解: ISP下载是51单片机,STM等单片机比较常见的一种下 ...

  2. Flex Metadata tags 元数据标签

    1.[Alternative] [可替换] 标明此类可以被参数中的类替换,版本号说明发生替换的版本. [Alternative]和[Deprecated] 不同.如果是[不建议使用]的类,以后的版本可 ...

  3. MySQL数据库导入外部*.sql文件具体步骤

    如需转载请标明出处:http://blog.csdn.net/itas109 环境: MySQL  50515 Navicat for MySQL 一.导入前提 确保导入的sql文件的host和por ...

  4. hdu 5445 Food Problem 多重背包

    Food Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5 ...

  5. 火狐HACK

     /*针对Firefox*/@-moz-document url-prefix() {    #nav{ width:200px; }} 

  6. UNIX标准化及实现之功能测试宏

    在头文件中定义了很多POSIX.1和XSI的符号.但是除了POSIX.1和XSI的定义之外,大多数实现在这些头文件中也加上了它们自己的定义.如果在编译一个程序时,希望它只使用POSIX定义而不使用任何 ...

  7. Gaussian Discriminant Analysis

    如果在我们的分类问题中,输入特征$x$是连续型随机变量,高斯判别模型(Gaussian Discriminant Analysis,GDA)就可以派上用场了. 以二分类问题为例进行说明,模型建立如下: ...

  8. linux 查看文件命令总结

    linux 查看文件命令总结 1.cat 查看文件内容 选项-b 空白行不显示行号.-n,空白行显示 2.more 查看文件内容,通过空格键查看下一页 q键退出查看 3.less 和上同,多了方向键( ...

  9. 基于动态库的C++插件开发模型

    基类为抽象类,在不同的动态库中实现不同的执行行为,但是每个动态库要提供2个统一的方法:1) baseClass * create(); 2) void destroy( baseClass* );,调 ...

  10. 关于automatic_Panoramic_Image_Stitching_using_Invariant_features 的阅读笔记

    并没有都读完,不过感觉还是有必要做一个笔记的,毕竟这只是随笔不是文章,所以可以有多少写多少,也算是工作总结了,最重要的是这个好在可以,完成所有有意义文档的检索,比起自己的word来说高级很多~~~. ...