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

二维单调栈

递增栈

相关推荐
小欣加油4 小时前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
3Cloudream4 小时前
LeetCode 003. 无重复字符的最长子串 - 滑动窗口与哈希表详解
算法·leetcode·字符串·双指针·滑动窗口·哈希表·中等
林木辛8 小时前
LeetCode热题 42.接雨水
算法·leetcode
黑菜钟11 小时前
代码随想录第七天|● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 18.四数之和
c++·算法·leetcode
pzx_00112 小时前
【LeetCode】14. 最长公共前缀
算法·leetcode·职场和发展
songx_9913 小时前
leetcode10(跳跃游戏 II)
数据结构·算法·leetcode
1白天的黑夜116 小时前
哈希表-49.字母异位词分组-力扣(LeetCode)
c++·leetcode·哈希表
愚润求学17 小时前
【贪心算法】day7
c++·算法·leetcode·贪心算法
共享家95271 天前
优先搜索(DFS)实战
算法·leetcode·深度优先
flashlight_hi2 天前
LeetCode 分类刷题:2563. 统计公平数对的数目
python·算法·leetcode