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

二维单调栈

递增栈

相关推荐
_日拱一卒36 分钟前
LeetCode:22括号生成
算法·leetcode·职场和发展
洛水水1 小时前
【力扣100题】88.多数元素
数据结构·算法·leetcode
洛水水1 小时前
【力扣100题】87.只出现一次的数字
数据结构·算法·leetcode
风筝在晴天搁浅2 小时前
LeetCode CodeTop 82.删除排序链表中的重复元素Ⅱ
算法·leetcode·链表
洛水水2 小时前
【力扣100题】84.字符串解码
算法·leetcode·职场和发展
洛水水3 小时前
【力扣100题】89.下一个排列
数据结构·算法·leetcode
洛水水3 小时前
【力扣100题】90.寻找重复数
算法·leetcode·职场和发展
alphaTao3 小时前
LeetCode 每日一题 2026/6/8-2026/6/14
算法·leetcode
想吃火锅100513 小时前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
小林ixn15 小时前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表