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

二维单调栈

递增栈

相关推荐
小白菜又菜12 小时前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
摸个小yu16 小时前
【力扣LeetCode热题h100】链表、二叉树
算法·leetcode·链表
skywalker_1117 小时前
力扣hot100-5(盛最多水的容器),6(三数之和)
算法·leetcode·职场和发展
生信研究猿17 小时前
leetcode 226.翻转二叉树
算法·leetcode·职场和发展
XWalnut18 小时前
LeetCode刷题 day9
java·算法·leetcode
6Hzlia18 小时前
【Hot 100 刷题计划】 LeetCode 39. 组合总和 | C++ 回溯算法与 startIndex 剪枝
c++·算法·leetcode
宵时待雨18 小时前
优选算法专题1:双指针
数据结构·c++·笔记·算法·leetcode
We་ct19 小时前
LeetCode 172. 阶乘后的零:从暴力到最优,拆解解题核心
开发语言·前端·javascript·算法·leetcode·typescript
老虎062719 小时前
LeetCode热题100 刷题笔记(第五天)双指针法 「 三数之和 」
笔记·算法·leetcode
美式请加冰19 小时前
简单多状态问题
数据结构·算法·leetcode