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

二维单调栈

递增栈

相关推荐
做怪小疯子1 分钟前
LeetCode 热题 100——哈希——字母异位词分组
算法·leetcode·哈希算法
小欣加油33 分钟前
leetcode 474 一和零
c++·算法·leetcode·职场和发展·动态规划
爱coding的橙子6 小时前
每日算法刷题Day84:11.11:leetcode 动态规划9道题,用时2h
算法·leetcode·动态规划
Kt&Rs8 小时前
11.11 LeetCode 题目汇总与解题思路
算法·leetcode·哈希算法
py有趣8 小时前
LeetCode算法学习之有效的字母异位词
学习·算法·leetcode
蒙奇D索大15 小时前
【算法】递归算法的深度实践:从布尔运算到二叉树剪枝的DFS之旅
笔记·学习·算法·leetcode·深度优先·剪枝
小白程序员成长日记17 小时前
2025.11.10 力扣每日一题
数据结构·算法·leetcode
dragoooon3419 小时前
[优选算法专题六.模拟 ——NO.40~41 外观数列、数青蛙]
数据结构·算法·leetcode
一匹电信狗20 小时前
【C++】封装红黑树实现map和set容器(详解)
服务器·c++·算法·leetcode·小程序·stl·visual studio
flashlight_hi1 天前
LeetCode 分类刷题:141. 环形链表
javascript·算法·leetcode