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 小时前
双指针算法
java·算法·leetcode
sjsjs111 小时前
【数据结构-差分】力扣1589. 所有排列中的最大和
数据结构·算法·leetcode
孙小二写代码1 小时前
[leetcode刷题]面试经典150题之4删除有序数组中的重复项II(中等)
算法·leetcode·面试
西柚与蓝莓3 小时前
922. 按奇偶排序数组 II 双指针 力扣
数据结构·算法·leetcode
Amor风信子3 小时前
【力扣】2376. 统计特殊整数
算法·leetcode·职场和发展
杰九12 小时前
【算法题】46. 全排列-力扣(LeetCode)
算法·leetcode·深度优先·剪枝
manba_12 小时前
leetcode-560. 和为 K 的子数组
数据结构·算法·leetcode
liuyang-neu12 小时前
力扣 11.盛最多水的容器
算法·leetcode·职场和发展
忍界英雄12 小时前
LeetCode:2398. 预算内的最多机器人数目 双指针+单调队列,时间复杂度O(n)
算法·leetcode·机器人
Tisfy12 小时前
LeetCode 2398.预算内的最多机器人数目:滑动窗口+单调队列——思路清晰的一篇题解
算法·leetcode·机器人·题解·滑动窗口