【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Example 1:


Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 6
Explanation: The maximal rectangle is shown in the above picture.
Example 2:
Input: matrix = []
Output: 0
Example 3:
Input: matrix = [["0"]]
Output: 0
Example 4:
Input: matrix = [["1"]]
Output: 1
Example 5:
Input: matrix = [["0","0"]]
Output: 0
参考84题的单调栈解法
求最大矩形=》求解包含当前柱子的最大矩阵=》就两端第一个小于该柱子的索引位置=>利用单调递增栈 (两端初始化一个0)
单调递减栈可以用来求两端第一个大于该柱子的索引值 (两端初始化一个极大值) 便于后面弹栈
CPP 代码
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
// 利用单调栈进行求解
if(matrix.size()==0) return 0;
int m=matrix.size(),n=matrix[0].size();
vector<int> dp(n,0);
int res=0;
for(int i=0;i<m;++i){
dp.resize(matrix[i].size());
for(int j=0;j<n;++j){
if(matrix[i][j]=='0'){
dp[j]=0;
}
else{
dp[j]=dp[j]+1;
}
}
int tmp=calrect(dp);
res=max(res,tmp);
}
return res;
}
int calrect(vector<int> dp){
dp.push_back(0);
dp.insert(dp.begin(),0);
stack<int>ss;
int n=dp.size();
int res=0;
for(int i=0;i<n;++i){
while(ss.size() && dp[ss.top()]>dp[i]){
int tmp=ss.top();ss.pop();
res=max(res,dp[tmp]*(i-ss.top()-1));
}
ss.push(i);
}
return res;
}
};
Python 代码
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
#这题如何暴力法求解呢?
#能不能换成柱子 好像不行哎
#利用单调栈
if not matrix:
return 0
m=len(matrix)
n=len(matrix[0])
stack=[0]*n
res=0
for i in range(m):
for j in range(n):
if matrix[i][j]=="0":
stack[j]=0
else:
stack[j]+=1
#启用单调栈来计算当前柱子的最大面积
are=self.calrect(stack)
res=max(are,res) return res def calrect(self,height): #单调栈求最大面积
height=[0]+height+[0]
stack=[]#维护一个单调递增栈 统计每个柱子两边第一个比他小的柱子
n=len(height)
res=0
for i in range(n):
while stack and height[stack[-1]]>height[i]:
tmp=stack.pop()
are=(i-stack[-1]-1)*height[tmp]
res=max(res,are)
stack.append(i)
return res
【leetcode】85. Maximal Rectangle(单调栈)的更多相关文章
- leetcode Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- [LeetCode] 85. Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- leetCode 85.Maximal Rectangle (最大矩阵) 解题思路和方法
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- leetcode[85] Maximal Rectangle
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- 刷题85. Maximal Rectangle
一.题目说明 题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积.难度是Hard! 二.我的解答 看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向 ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
随机推荐
- spring mvc 原理(快速理解篇)
这两张图大家应该都不陌生. 从图上来看就是:一个请求过来,front controller根据具体的请求路径分派到具体的controller,具体的controller处理请求并把处理结果返回给fro ...
- 2021.11.2-测试T1数独
痛苦 题目 数 独 [问题描述] 给定一个9*9矩阵,对其进行几种操作,分别是插入,删除,合并,查询,输出 主要学到了一些特别的操作. (1)备份( 本蒟蒻第一次了解到) (2)对与数据的一些特别的改 ...
- Vue首屏性能优化组件
Vue首屏性能优化组件 简单实现一个Vue首屏性能优化组件,现代化浏览器提供了很多新接口,在不考虑IE兼容性的情况下,这些接口可以很大程度上减少编写代码的工作量以及做一些性能优化方面的事情,当然为了考 ...
- 史上最简单的排序算法?看起来却满是bug
大家好,我是雨乐. 今天在搜论文的时候,偶然发现一篇文章,名为<Is this the simplest (and most surprising) sorting algorithm ever ...
- kubernetes笔记
如果pod包含多个container, 这些container不会跨机器分布 每个container只运行一个进程,而不是在一个container运行多个进程,这样更容易处理进程异常重启,进程日志等问 ...
- Merge into用法总结
简单的说就是,判断表中有没有符合on()条件中的数据,有了就更新数据,没有就插入数据. 有一个表T,有两个字段a.b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在 ...
- 菜鸟Markdown笔记,看这个就够了
菜鸟markdown语法笔记 1.标题 写法:共六级标题,一个#是一级标题,两个#是二级标题,三个#是三级标题······以此类推 (#)+空格键,快捷方式是Ctrl+1/2/3/4/5/6 2.段落 ...
- docker详细
镜像(image) 容器(container) 启动,删除,停止 仓库(repository) docker images
- C/C++ Qt TreeWidget 嵌套节点操作技巧
在上一篇博文<C/C++ Qt TreeWidget 单层树形组件应用>中给大家演示了如何使用TreeWidget组件创建单层树形结构,并给这个树形组件增加了右键菜单功能,接下来将继续延申 ...
- 32、最长有效括号 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(32)最长有效括号 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "滑动窗 ...