85. Maximal Rectangle

85. Maximal Rectangle

python 复制代码
class Solution:
    def maximalRectangle(self, matrix: List[List[str]]) -> int:
        def hist(heights):
            stack,ans=[],0
            for i,h in enumerate(heights+[0]):
                while stack and heights[stack[-1]]>=h:
                    H=heights[stack.pop()]
                    W=i if not stack else i-stack[-1]-1
                    ans=max(ans,H*W)
                stack.append(i)
            return ans

        if not matrix or not matrix[0]: return 0

        m,n,ans=len(matrix[0]),len(matrix),0
        row=[0]*m
        for i in range(n):
            for j in range(m):
                row[j]=0 if matrix[i][j]=='0' else row[j]+1
            ans=max(ans,hist(row))
        return ans

二维单调栈

递增栈

相关推荐
DARLING Zero two♡4 小时前
【优选算法】D&C-Mergesort-Harmonies:分治-归并的算法之谐
java·数据结构·c++·算法·leetcode
Q741_1475 小时前
C++ 分治 归并排序 归并排序VS快速排序 力扣 912. 排序数组 题解 每日一题
c++·算法·leetcode·归并排序·分治
熬了夜的程序员15 小时前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
dragoooon3417 小时前
[优选算法专题四.前缀和——NO.31~32 连续数组、矩阵区域和]
数据结构·算法·leetcode·1024程序员节
熬了夜的程序员21 小时前
【LeetCode】87. 扰乱字符串
算法·leetcode·职场和发展·排序算法
·白小白1 天前
力扣(LeetCode) ——15.三数之和(C++)
c++·算法·leetcode
海琴烟Sunshine1 天前
leetcode 268. 丢失的数字 python
python·算法·leetcode
仰泳的熊猫1 天前
LeetCode:268. 丢失的数字
数据结构·c++·算法·leetcode
VT.馒头1 天前
【力扣】2725. 间隔取消
javascript·leetcode·1024程序员节
咪咪渝粮1 天前
108. 将有序数组转换为二叉搜索树
算法·leetcode