题目
给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。
求在该柱状图中,能够勾勒出来的矩形的最大面积。
代码
小结:参考https://www.cnblogs.com/ariel-dreamland/p/9159207.html 容易理解核心思路
class Solution(object):
def largestRectangleArea(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
res = 0
s = []
heights = [0] + heights + [0]
for i in range(len(heights)):
while s and heights[s[-1]] > heights[i]:
cur = s.pop()
width = i - s[-1] - 1
res = max(res, heights[cur] * width)
s.append(i)
return res