leetcode刷题 83 84

    xiaoxiao2023-11-16  158

    from collections import deque class Solution: def largestRectangleArea(self, heights: List[int]) -> int: max_area = 0 stack = deque() for x in heights: ch = 1 while len(stack) > 0 and x < stack[-1][0]: v, h = stack.pop() max_area = max(max_area, v*(h+ch-1)) ch += h stack.append([x, ch]) th = 0 while len(stack) > 0: v, h = stack.pop() th += h max_area = max(max_area, v*th) return max_area
    class Solution(object): def largestRectangleArea(self, heights): heights.append(0) stackk = [] res = 0 for i in range(len(heights)): index = i while stackk and stackk[-1][1] > heights[i]: (index,height) = stackk.pop() res = max(res,height * (i-index)) stackk.append((index,heights[i])) return res 刘润森! 认证博客专家 Python Java 前端 17年就读于东莞XX学院化学工程与工艺专业,GitChat作者。Runsen的微信公众号是"Python之王",因为Python入了IT的坑,从此不能自拔。公众号内容涉及Python,Java计算机、杂谈。干货与情怀同在。喜欢的微信搜索:「Python之王」。个人微信号:RunsenLiu。不关注我公号一律拉黑!!!
    最新回复(0)