Data Structures19 sections · 729 units
Open in Course

A Famous Problem

Histogram rectangles

Given an array of bar heights representing a histogram, find the area of the largest rectangle that can be formed.

Example: heights =[2,1,5,6,2,3]= [2, 1, 5, 6, 2, 3] Answer: 1010 (rectangle of height 55 and width 22 using bars at indices 22 and 33) You're solving a classic interview problem.

Brute force is O(n2)O(n^2). The monotonic stack solution is O(n)O(n).