Largest rectangle in a histogram

heights lists bar heights of width one. Write largest_rectangle(heights) that returns the area of the largest rectangle that fits entirely inside the bars.

largest_rectangle([2, 1, 5, 6, 2, 3])  ->  10     (5 and 6, height 5, width 2)
largest_rectangle([2, 4])              ->  4
largest_rectangle([])                  ->  0

A stack of indices with increasing heights. When a lower bar arrives, every taller bar on the stack has found its right boundary; pop it and compute its rectangle, with the left boundary being the new stack top. A sentinel of height 0 at the end flushes the stack.

def largest_rectangle(heights):
    ...