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

二维单调栈

递增栈

相关推荐
吗~喽10 小时前
【LeetCode】四数之和
算法·leetcode·职场和发展
卿言卿语10 小时前
CC1-二叉树的最小深度
java·数据结构·算法·leetcode·职场和发展
小欣加油11 小时前
leetcode 329 矩阵中的最长递增路径
c++·算法·leetcode·矩阵·深度优先·剪枝
Emilia486.11 小时前
【Leetcode&nowcode&数据结构】单链表的应用(初阶)
c语言·数据结构·算法·leetcode
仰泳的熊猫12 小时前
LeetCode:700. 二叉搜索树中的搜索
数据结构·c++·算法·leetcode
代码充电宝12 小时前
LeetCode 算法题【中等】189. 轮转数组
java·算法·leetcode·职场和发展·数组
微笑尅乐13 小时前
从递归到迭代吃透树的层次——力扣104.二叉树的最大深度
算法·leetcode·职场和发展
im_AMBER13 小时前
Leetcode 28
算法·leetcode
And_Ii13 小时前
LeetCode 3350. 检测相邻递增子数组 II
数据结构·算法·leetcode
冷月葬花~14 小时前
day24
数据结构·算法·leetcode