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

二维单调栈

递增栈

相关推荐
兰令水35 分钟前
hot100【acm版】【2026.7.13打卡-java版本】
java·开发语言·数据结构·算法·leetcode·面试
Tisfy41 分钟前
LeetCode 1291.顺次数:打表/枚举
算法·leetcode·题解·枚举·遍历
罗超驿2 小时前
双指针算法详解:从入门到精通(Java版)
算法·leetcode·职场和发展
tkevinjd2 小时前
力扣300-最长递增子序列
算法·leetcode·职场和发展·动态规划·贪心
tkevinjd5 小时前
416分割等和子集
java·python·算法·leetcode·职场和发展
什巳17 小时前
JAVA练习278- 和为 K 的子数组
java·学习·算法·leetcode
鱼儿也有烦恼20 小时前
快速学完 LeetCode top 1~50
leetcode·algorithm
退休倒计时1 天前
【每日一题】LeetCode 17. 电话号码的字母组合 TypeScript
算法·leetcode·typescript
临沂堇1 天前
刷题日志 | LeetCode Hot 100 双指针
算法·leetcode·职场和发展
XWalnut1 天前
LeetCode刷题 day29
java·算法·leetcode