作者: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. jboss 7部署cas3.4.11

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. 理解 auto_ptr<T>

    1.auto_ptr<T>解决什么问题? 在堆上获取的资源,客户可能会忘记delete,或者由于异常没有执行到delete,导致资源泄漏.在栈上分配的对象,有个特点,不论出现什么情况,超出 ...

  3. android120 zhihuibeijing 开机页面

    软件启动旋转动画: 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  4. Mysql在windows下的免安装配置步骤和重新安装的步骤

    windows下mysql免安装配置 1. 下载mysql免安装压缩包 下载mysql-5.6.22-winx64.zip 解压到本地D:\mysql-5.6.22-winx64 2. 修改配置文件 ...

  5. php笔记02:整型细节说明

    1.php的一个整数可以是十进制,也可以是八进制和十六进制: 比如:$a=0123; //八进制 $a=0x1A; //十六进制 2.php的整数都是有符号的数(java也是只有有符号数) 3.在ph ...

  6. mapping 详解3(Meta-Fields)

    文档标识相关元数据字段 _index 当执行多索引查询时,可能需要添加特定的一些与文档有关联的索引的子句. _index 字段可以用在 term.terms 查询,聚合(aggregations)操作 ...

  7. FastDFS详解

    1.FastDFS是什么 FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过 专有API对文件进行存取访问 ...

  8. BLOCKED和WAITING的区别

    /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state ...

  9. 【转载】TCL装载包和版本控制

    转载来源:http://blog.chinaunix.net/uid-9967220-id-3033702.html package forget ?package package ...?      ...

  10. 安卓开发错误:The type android.support.v4.app.TaskStackBuilder$SupportParentable cannot be resolved.

    今天在使用低版本下的ActionBar,在继承ActionBarActivity时报了"The type Android.support.v4.app.TaskStackBuilder$Su ...