作者: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. 人工智能-有限状态机(FSM)的学习

    首先声明:此文源于本人最近学习的一本书 <游戏人工智能编程案例精粹> FSM的定义: 一个有限状态机是一个设备,或是一个设备模型,具有有限数量的状态,它可以在任何给定的时间根据输入进行操作 ...

  2. spring3.2.2+mybatis3.2.3+c3p0项目整合

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

  3. xcode 4 制作静态库详解

    合并.a文件,制作通用静态库 这二个库一个是用于真机运行的一个是用于模拟器运行的.其实我们可以利用lipo将这二个文件打包成一个通用的a文件.命令如下: 将/Users/user/Library/De ...

  4. WM_VSCROLL

    关键点 控制滚动条在最下面 实现过程 SendMessage(form1.Memo1.Handle,WM_VSCROLL,SB_BOTTOM,0); 图 备注 相关链接   来自为知笔记(Wiz)

  5. Java基础 Day14 泛型

    //为什么要使用泛型 //1.解决元素存储的安全性的问题 //2.解决获取元素时,须要类型转换的问题 //未使用泛型 package org.tizen.test; import java.util. ...

  6. 在centos上使用yum安装redis及php扩展php-redis

    .wget http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-rele ...

  7. C栈stack

    栈是一种  特殊的线性表 栈仅能在线性表的一端进行操作 栈顶(Top):允许操作的一端 栈底(Bottom):不允许操作的一端 Stack的常用操作 创建栈 销毁栈 清空栈 进栈 出栈 获取栈顶元素 ...

  8. TIME-WAIT和CLOSE-WAIT

    系统调优,你所不知道的TIME_WAIT和CLOSE_WAIT 2016-03-11 运维帮 来源微信订阅号:大房说 作者:大房 你遇到过TIME_WAIT的问题吗?   我相信很多都遇到过这个问题. ...

  9. kernel debuging

    http://blog.csdn.net/XscKernel/article/category/1276234

  10. mysql中的存储过程和事务隔离

    ※存储过程存储过程是保存在数据库上的一段可执行代码.1.定义存储过程的语法是:Create procedure sp_name (参数..)Begin SQL语句End;2.调用它的方法:Call s ...