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

二维单调栈

递增栈

相关推荐
Hi李耶4 小时前
【LeetCode】17.电话号码的字母组合
算法·leetcode·职场和发展
Forever Nore14 小时前
LeetCode 4 寻找两个正序数组的中位数 - 二分
算法·leetcode
evans在进步1 天前
LeetCode 200:岛屿数量——Java DFS 染色法详解
java·leetcode·深度优先
旖旎夜光1 天前
LeetCode 202:快乐数(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
君君思密达1 天前
Leetcode Hot 100 题目详解-哈希表
数据结构·算法·leetcode
203号居民1 天前
LeetCode hot 100 —560. 和为 K 的子数组
java·算法·leetcode
退休倒计时1 天前
【每日一题】LeetCode 20. 有效的括号 TypeScript
算法·leetcode·职场和发展·typescript
.道阻且长.1 天前
3.LeetCode算法习题讲解--双指针--快乐数
算法·leetcode·职场和发展
Forever Nore1 天前
LeetCode 5 最长回文子串 - 中心扩散
linux·算法·leetcode
Forever Nore1 天前
LeetCode 6 Z 字形变换 - 按行模拟
算法·leetcode