<p>Given an array of integers <code>heights</code> representing the histogram&#39;s bar height where the width of each bar is <code>1</code>, return <em>the area of the largest rectangle in the histogram</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/histogram.jpg" style="width: 522px; height: 242px;" />
<pre>
<strong>Input:</strong> heights = [2,1,5,6,2,3]
<strong>Output:</strong> 10
<strong>Explanation:</strong> The above is a histogram where width of each bar is 1.
The largest rectangle is shown in the red area, which has an area = 10 units.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/histogram-1.jpg" style="width: 202px; height: 362px;" />
<pre>
<strong>Input:</strong> heights = [2,4]
<strong>Output:</strong> 4
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= heights.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= heights[i] &lt;= 10<sup>4</sup></code></li>
</ul>

# in: int[] heights
# out: largest rectangle in histogram

# can be combined from different bars
# all ints
# all positive

# we could have a bar of width 1 that is so tall it is the biggest
# at any given point, the max area is the smallest bar to the left/right times width
# we could expand out from each bar and compare max area at each point
# what if we tried pointers?
# come in from left + right and at each point, we consider the best possible area
# we can essentially shrink the taller nums to the left once we hit a smaller num
# we could build a new array of increasing nums from left and right
# at each num, we essentially need to know the left + right index that it can stretch to
# since we want to account for the max we can go to the left, what if we use a stack?
# do we need a second stack to the right? maybe
# go across the array, then when we get to a num that is smaller than the last (or min so far?)
# we should track indices of nums as well so we know how far to the left a height goes
# ok so use a stack, then pop so we get back to something shorter than prev num
# each time we hit a shorter num, we know the height to the left has reached its best chance
# multiply the top num by the distance to the next top el in the stack
# keep popping until we reach something shorter thasn right num
# keep going to end of list

# T: O(N), moving across array once and popping up to once (2N)
# S: O(N) for stack

class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        if not heights:
            return 0
        max_area = 0
        stack = []
        for i in range(len(heights)): # 0,9
            height = heights[i] # 9
            while stack and height < heights[stack[-1]]:
                comparison_height = heights[stack.pop()]
                width = i
                if stack:
                    width = i - stack[-1] - 1
                area = comparison_height * width
                max_area = max(max_area, area)
            stack.append(i) #[0,0, 9,1]

        # last nums
        while stack:
            comparison_height = heights[stack.pop()] # 9
            width = len(heights) #
            if stack:
                width = len(heights) - stack[-1] - 1 # 1
            area = comparison_height * width
            max_area = max(max_area, area)

        return max_area



31600