Core Idea
For each bar, the largest rectangle using that bar as the minimum height extends until a smaller bar appears on each side.
Algorithm
Use a monotonic increasing stack of bar indices. When a shorter bar arrives, pop bars and compute the rectangle width determined by the new stack top.
Common Mistakes
Add a sentinel height 0 at the end to flush the stack. Use 64-bit area because height times width can be large.