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

二维单调栈

递增栈

相关推荐
圣保罗的大教堂2 小时前
leetcode 1406. 石子游戏 III 困难
leetcode
带多刺的玫瑰10 小时前
Leecode#15刷题之三数之和
算法·leetcode·职场和发展
圣保罗的大教堂10 小时前
leetcode 877. 石子游戏 中等
leetcode
shehuiyuelaiyuehao12 小时前
算法32,连续数组,前缀和+哈希表
算法·leetcode·职场和发展
Navigator_Z14 小时前
LeetCode //C - 1223. Dice Roll Simulation
c语言·算法·leetcode
Tisfy14 小时前
LeetCode 2058.找出临界点之间的最小和最大距离:遍历+遇到极值则更新(这种题谁空间复杂度不是O(1)啊)
linux·数据库·leetcode·链表·题解·模拟·遍历
小欣加油15 小时前
Leetcode2058 找出临界点之间的最小和最大距离
数据结构·c++·算法·leetcode·职场和发展
CoderYanger15 小时前
A.每日一题:3345. 最小可整除数位乘积 I
java·数据结构·程序人生·算法·leetcode·面试·学习方法
玖玥拾16 小时前
LeetCode 1 两数之和
算法·leetcode·职场和发展
旖旎夜光16 小时前
LeetCode 974:和可被 K 整除的子数组(前缀和) —— 题解
数据结构·c++·算法·leetcode·前缀和