Skip to content

Commit ad3d1a7

Browse files
committed
Update 29. 柱状图中最大的矩形.md
1 parent af38222 commit ad3d1a7

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

29. 柱状图中最大的矩形.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ class Solution:
1313
#利用单调栈+哨兵,找到比当前高度小的左右最近索引。首尾加两个0,在结束后,栈一定为空!
1414
def largestRectangleArea(self, heights: List[int]) -> int:
1515
heights = [0] + heights + [0]
16+
#单调栈
1617
stack = []
1718
res = 0
1819
for i in range(len(heights)):
20+
#如果当前高度小于栈顶高度
1921
while stack and heights[i] < heights[stack[-1]]:
22+
#取出栈顶索引值
2023
tmp = stack.pop()
24+
#计算面积,栈顶高度*(右边界索引值-左边界索引值-1)
2125
res = max(res, (i-stack[-1]-1)*heights[tmp])
2226
stack.append(i)
2327
return res

0 commit comments

Comments
 (0)