Last updated
Last updated
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
def helper(start, end):
if start > end:
return 0
min_h_index = start
# searching space from start to end + 1
for i in range(start, end + 1):
if heights[i] < heights[min_h_index]:
min_h_index = i
return max(heights[min_h_index] * (end - start + 1 ), helper(start, min_h_index - 1), helper(min_h_index + 1, end))
return helper(0, len(heights) - 1)
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
n = len(heights)
if n == 0:
return 0
if len == 1:
return heights[0]
area = 0
stack = []
for i in range(n):
while stack and heights[stack[-1]] > heights[i]:
height = heights[stack.pop()]
while stack and heights[stack[-1]] == height:
stack.pop()
width = 0
if not stack:
width = i
else:
width = i - stack[-1] - 1
area = max(area, width * height)
stack.append(i)
while stack:
height = heights[stack.pop()]
while stack and heights[stack[-1]] == height:
stack.pop()
width = 0
if not stack:
width = n
else:
width = n - stack[-1] - 1
area = max(area, width * height)
return area
# stack = [-1]
# heights.append(0)
# ans = 0
# for i, height in enumerate(heights):
# while heights[stack[-1]] > height:
# h = heights[stack.pop()]
# w = i - stack[-1] - 1
# ans = max(ans, h*w)
# stack.append(i)
# return ans
# max_area = 0
# for i in range(len(heights)):
# min_height = float('inf')
# for j in range(i, len(heights)):
# min_height = min(min_height, heights[j])
# max_area = max(max_area, min_height * (j - i + 1))
# return max_area
# def helper(start, end):
# if start > end:
# return 0
# min_index = start
# for i in range(start, end + 1):
# if heights[min_index] > heights[i]:
# min_index = i
# return max(
# heights[min_index] * (end - start + 1),
# helper(start, min_index - 1),
# helper(min_index + 1, end),
# )
# return helper(0, len(heights) - 1)