1def largest_rectangle_area(heights):2 stack = []3 max_area = 04 for i in range(len(heights) + 1):5 while stack and (i == len(heights) or heights[stack[-1]] > heights[i]):6 j = stack.pop()7 width = i if not stack else (i - stack[-1] - 1)8 max_area = max(max_area, width * heights[j])9 stack.append(i)10 return max_area