Mental Model
We use a stack to keep track of bars that are not yet processed for the largest rectangle. When we find a bar shorter than the top of the stack, we calculate the area with the top bar as the smallest height.
Analogy: Imagine stacking books by height. When you find a shorter book, you remove taller books from the stack and calculate the largest flat surface you can make with those books before placing the shorter one.
Histogram bars: height
6 | ■
5 | ■ ■
4 | ■ ■ ■
3 | ■ ■ ■ ■
2 | ■ ■ ■ ■
1 | ■ ■ ■ ■
+---------
0 1 2 3 4 (indices)
Stack: stores indices of bars in increasing height order -> [0,1,2,...]