leetcode Maximal Rectangle 单调栈
作者: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 单调栈的更多相关文章
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- poj 2559 Largest Rectangle(单调栈)
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26549 ...
- 牛客多校第二场H Second Large Rectangle 单调栈or悬线法
Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...
- [每日一题2020.06.13]leetcode #739 #15 单调栈 双指针查找
739 每日温度 ( 单调栈 ) 题目 : https://leetcode-cn.com/problems/daily-temperatures/ 题意 : 找到数组每一个元素之后第一个大于它的元素 ...
- [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
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [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 ...
随机推荐
- 并查集类的c++封装,比較union_find algorithm四种实现方法之间的性能区别
问题描写叙述: 在计算机科学中,并查集是一种树型的数据结构,其保持着用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.有一个联合-查找算法(union-find algorithm ...
- android的ListView做表格添加圆角边框
边框,圆角,都可以实现的 在drawable目录下添加view_yuan_morelist.xml,设置控件的边框代码.如下: <?xml version="1.0" enc ...
- pomelo 初始化配置...
在创建app的时候会初始化master和server以及log配置.. /** * Initialize application configuration. */ module.exports.de ...
- mysql 源码调试方法
http://blog.itpub.net/29254281/viewspace-1847415/ 其中吕海波老师分享的内容是 <调试Oracle二三例:调试技术在日常运维中的应用>其中 ...
- linux定时执行文件
#!/bin/bashcurl "http://172.16.201.24:8080/?app_act=timer/automic_integral" -b -q -o /dev ...
- 在android的spinner中,实现取VALUE值和TEXT值
为了实现在android的spinner实现取VALUE值和TEXT值,我尝试过好些办法,在网上查的资料,都是说修改适配器,刚开始我也是通过修改适配器的方法来做的,但是如果一个activity有多个s ...
- 如何在C#中循环一个枚举
在C#中要想迭代循环一个枚举,最容易想到的办法是直接进行循环,如下代码所示: public enum Suit { Spades, Hearts, Clubs, Diamonds } publ ...
- Spring 的@Controller 和@RestController的区别
@RestController Spring 4.0中新增的特性 @RestController 继承自 @Controller,相当于@ResponseBody + @Controller 1. ...
- 滚动新闻插件vticker
vTicker 是一款非常小巧的 jQuery 垂直滚动插件,压缩后只有 2KB.vTicker 支持自定义滚动时间.间隔时间.显示个数.滚动方向(向上/向下).容器高度等等,但它对 HTML 结构有 ...
- 用js实现简单排序算法
一.冒泡排序 var arr1=[3,9,2,7,0,8,4]; for(var i=0;i<arr1.length;i++){ for(var j=i+1;j<arr1.length;j ...