【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址: https://leetcode.com/problems/maximal-rectangle/description/
题目描述:
Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.
Example:
Input:
[
["1","0","1","0","0"],
["1","0","1","1","1"],
["1","1","1","1","1"],
["1","0","0","1","0"]
]
Output: 6
题目大意
给出了一个二维的数组,求在这里面能够成的最大的矩形面积是多少。
解题方法
本以为会和221. Maximal Square做法一样使用DP解决,可是感觉状态转移方程太复杂,所以还是参考了84. Largest Rectangle in Histogram升级版的解法。
如图所示,如果把每一行的1和它上面的1连在一起,那么就可以看成一个个站里的矩形方块。那么我们的最终目的就是找出最大面积的矩形方块,所以就是第84题的做法了,使用单调栈。

需要注意的是,我们使用一个height数组,保存到某一层的第i个位置为止,能向上构成的矩形的高度。而且需要对每层都做一个寻找面积的操作,最终选择所有层中能够成矩形面积最大值。
时间复杂度是O(M(N+M)),空间复杂度是O(N)。
class Solution(object):
def maximalRectangle(self, matrix):
"""
:type matrix: List[List[str]]
:rtype: int
"""
if not matrix or not matrix[0]: return 0
M, N = len(matrix), len(matrix[0])
height = [0] * N
res = 0
for row in matrix:
for i in range(N):
if row[i] == '0':
height[i] = 0
else:
height[i] += 1
res = max(res, self.maxRectangleArea(height))
return res
def maxRectangleArea(self, height):
if not height: return 0
res = 0
stack = list()
height.append(0)
for i in range(len(height)):
cur = height[i]
while stack and cur < height[stack[-1]]:
w = height[stack.pop()]
h = i if not stack else i - stack[-1] - 1
res = max(res, w * h)
stack.append(i)
return res
参考资料:
https://www.jianshu.com/p/cffdc94c9c19
日期
2018 年 10 月 10 日 —— 冻成狗
【LeetCode】85. Maximal Rectangle 解题报告(Python)的更多相关文章
- LeetCode: Maximal Rectangle 解题报告
Maximal RectangleGiven a 2D binary matrix filled with 0's and 1's, find the largest rectangle contai ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 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 [含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
给定一个只含0和1的数组,求含1的最大矩形面积. Given a 2D binary matrix filled with 0's and 1's, find the largest rectangl ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- idea中如何找到重写
Ctrl+O 为了避免写错重写类和快速重写.
- 关于写SpringBoot+Mybatisplus+Shiro项目的经验分享三:问题2
框架: SpringBoot+Mybatisplus+Shiro 简单介绍:关于写SpringBoot+Mybatisplus+Shiro项目的经验分享一:简单介绍 搜索框是该项目重要的一环,由于涉及 ...
- 论文解读(SDNE)《Structural Deep Network Embedding》
论文题目:<Structural Deep Network Embedding>发表时间: KDD 2016 论文作者: Aditya Grover;Aditya Grover; Ju ...
- linux vi和vim编辑器
所有的Linux系统都会内建vi文本编辑器,vim具有程序编辑的能力,可以看作是vi的增强版本 三种常见模式 正常模式 以vim打开一个文档直接进入的模式,快捷键可以使用. 1.这个模式可以使用上下左 ...
- 并发 并行 进程 线程 协程 异步I/O python async
一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- mongDB进阶
Mongo进阶 聚合 聚合操作将来自多个文档的值组合在一起,并且可以对分组数据执行各种操作以返回单个结果. 文档进入多阶段管道,将文档转换为聚合结果 聚合管道 例子: 第一阶段:过滤,$match 第 ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 3.7 rust 静态块
Cargo.toml [dependencies] lazy_static = "1.4.0" main.rs #[macro_use] extern crate lazy_sta ...
- 删除button中除label之外的View
因为button中的UIButtonLabel判断class类型时,会被认为是view,所以想删除view类型的子控件时,会将label也删掉,从而无法显示title,此时,可以给指定的View添加t ...