Data Structures19 sections · 729 units
Open in Course

Histogram - Visualization

Trace the algorithm

Heights: [2,1,5,6,2,3][2, 1, 5, 6, 2, 3]. Maintain increasing stack of indices. i=0i=0, h=22: push 00. Stack: [0][0]

i=1i=1, h=11: 1<21 < 2, pop 00. Area = 2×(1(1)1)=22 \times (1 - (-1) - 1) = 2.

Push 11. Stack: [1][1]

i=2i=2, h=55: push 22. Stack: [1,2][1, 2]

i=3i=3, h=66: push 33. Stack: [1,2,3][1, 2, 3]

i=4i=4, h=22: pop 33, area = 6×1=66 \times 1 = 6.

Pop 22, area = 5×2=105 \times 2 = 10. Push 44. Stack: [1,4][1, 4]

i=5i=5, h=33: push 55. Stack: [1,4,5][1, 4, 5]

Pop remaining: areas 33, 88, 66. Maximum: 1010.