Largest rectangle under a bar chart
Asked at Citadel, HRT
Given bar heights (for example depth at each price level of an order book), find the area of the largest rectangle that fits entirely under the bars.
heights = [2, 1, 5, 6, 2, 3]
-> 10 # the bars of height 5 and 6 span width 2: 5 x 2 = 10
Return the maximum rectangle area. The brute force over all (left, right) pairs is ; aim for .
Show a hint
Every maximal rectangle is capped by some bar that is its shortest bar. For a fixed bar as the limiting height, how far left and right can the rectangle stretch before it hits something shorter?
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.