- class Solution:
- def largestRectangleArea(self, heights: List[int]) -> int:
- # st = [0]
- # result = 0
- # for i in range(1,len(heights)):
- # if heights[i] >= heights[st[-1]]:
- # st.append(i)
- # else:
- # while st and heights[i]
- # mid = st.pop()
- # if st:
- # result =max(result,heights[mid]*(i-st[-1]-1))
- # else:
- # result = max(result,heights[mid]*(i))
-
- # st.append(i)
- # if st:
- # for j in range(len(st)-1,0,-1):
- # result =max(result,(len(heights)-st[j-1]-1)*heights[st[j]])
- # result =max(result,len(heights)*heights[st[0]])
-
- # return result
- st = [0]
- result = 0
- heights.append(0)
- heights.insert(0,0)
- for i in range(1,len(heights)):
- if heights[i] >= heights[st[-1]]:
- st.append(i)
- else:
- while st and heights[i]
1]]: - mid = st.pop()
- if st:
- result =max(result,heights[mid]*(i-st[-1]-1))
- st.append(i)
- return result