We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent af38222 commit ad3d1a7Copy full SHA for ad3d1a7
29. 柱状图中最大的矩形.md
@@ -13,11 +13,15 @@ class Solution:
13
#利用单调栈+哨兵,找到比当前高度小的左右最近索引。首尾加两个0,在结束后,栈一定为空!
14
def largestRectangleArea(self, heights: List[int]) -> int:
15
heights = [0] + heights + [0]
16
+ #单调栈
17
stack = []
18
res = 0
19
for i in range(len(heights)):
20
+ #如果当前高度小于栈顶高度
21
while stack and heights[i] < heights[stack[-1]]:
22
+ #取出栈顶索引值
23
tmp = stack.pop()
24
+ #计算面积,栈顶高度*(右边界索引值-左边界索引值-1)
25
res = max(res, (i-stack[-1]-1)*heights[tmp])
26
stack.append(i)
27
return res
0 commit comments