【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- ...
随机推荐
- xiaoxiaole
common.cpp #include "common.h" common.h #ifndef COMMON_H_INCLUDED #define COMMON_H_INCLUDE ...
- Serverless 工程实践|自建 Apache OpenWhisk 平台
作者 | 刘宇(江昱) 前言:OpenWhisk 是一个开源.无服务器的云平台,可以在运行时容器中通过执行扩展的代码响应各种事件,而无须用户关心相关的基础设施架构. OpenWhisk 简介 Open ...
- cm2 逆向分析
目录 cm2 逆向分析 前言 查壳 逆向分析 encrypt函数 POC代码 cm2 逆向分析 前言 这是逆向实战之CTF比赛篇的第3篇,在这里我就不再讲的特别小白了,有些简单操作可能会略过. 查壳 ...
- 从0到1搭建自己的组件(vue-code-view)库(上)
0x00 前言 本文将从结构.功能等方面讲解下项目 vue-code-view 的搭建过程,您可以了解以下内容: 使用 vue cli 4从0搭建一个组件库及细致配置信息. 项目的多环境构建配置. 项 ...
- linux堡垒机下定位日志文件内容
查找关键词grep 命令: grep '关键字' 文件 --color 功能:搜素文件内容 语法: grep [-iv] 关键字 文件 -i 不区分大小写 -v 忽略指定字符串 -n 显示行号 -C ...
- 自定义 axios
自定义 axios function axios({ url, method = 'GET', params = {}, data = {} }) { // 返回一个 promise 对象 retur ...
- 3D 穿梭效果?使用 UWP 也能搞定
昨天 ChokCoco 大佬搞了个 3D 穿梭效果出来,具体可见这里: 3D 穿梭效果?使用 CSS 轻松搞定 这个效果太神奇了,他还问我能不能用 WPF 搞出来,因为我完全没用过 WPF 的 3D, ...
- [第二章]c++学习笔记2(类和对象的基础3)
隐藏的概念 隐藏的作用 使用例 成员函数的重载与缺省(附使用例) 注意事项
- sql关联修改
UPDATE p set p.ClientID = c.id from PaymentTable p left JOIN ClientTable c on p.ClientID = c.Clie ...
- tomcat隐藏版本号
默认报错页面信息会暴露出版本号 进入tomcat的lib目录找到catalina.jar文件 unzip catalina.jar之后会多出两个文件夹 进入org/apache/catalina/ut ...