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 ...
随机推荐
- WinFrom 安装包制作
1.添加安装向导项目打开文件系统界面,选择应用程序文件夹.在右侧右击->添加->文件,把程序需要的文件都添加进来. 2.右击程序集->创建快捷方式.右击快捷方式->属性窗口-& ...
- JAVA线程全局异常处理
大家平时写线程很多,但可能很少关注如何捕获线程的全局异常.其实jdk提供了两种捕获全局异常的方法,一种是基于整个线程类(staticsetDefaultUnaughtExceptionHandler( ...
- 理解shared_ptr<T>
1.shared_ptr<T>解决什么问题? auto_ptr有个局限,拥有权转移.这往往不符合我们的需求,有时候我们期望,多个资源管理对象可以共享一个资源,当引用计数为0的时候,执行de ...
- MFC——AfxParseURL用法
1.功能: 该函数解析URL字符串并返回服务的类型及组件,包含在 afxinet.h 头文件中. 2.定义 BOOL AFXAPI AfxParseURL(LPCTSTRpstrURL,DWORD&a ...
- 【ALearning】第三章 Android基本常见控件
本章主要介绍主要的寻常较多使用的控件,包含TextView.EditView.ImageView.Button等.本章将介绍相关控件基本属性的使用,为以后章节的进阶学习提供基础.案例中引用的Linea ...
- android学习日记0--开发需要掌握的技能
一.开发android,我们需要哪些技能基础 1.Java基础知识 2.Linux基础知识 3.数据库基础知识 4.网络协议 5.Android基础知识 6.服务器端开发知识 1.Java基础知识 很 ...
- interactive_timeout
[mysqld] interactive_timeout 交互式连接 会话1 [root@localhost ~]# mysql -umysql -p Enter password: Welcome ...
- [转]javascript指定事件处理程序包括三种方式:
javascript指定事件处理程序包括三种方式: (1):DOM0级事件处理程序 如: 代码如下: var btn=document.getElementById("mybtn" ...
- truncate和 delete的区别:
1.一个表要删除全部数据如何实现:①delete,②truncate EG:删除a表中的所有数据: CREATE TABLE `b` ( `id` int(11) NOT NULL AUTO_INCR ...
- C++获取文件长度
ifstream fin("example.txt", ios::binary); fin.seekg(0, ios::end); // 设置指针到文件流尾部 streampos ...