Data Structures19 sections · 729 units
Open in Course

Histogram - Why It Works

Correctness argument

When you pop bar jj because bar ii is shorter:

1.1. Bar jj can't extend right past index i1i - 1 (bar ii is shorter).

2.2. Bar jj can't extend left past the new stack top (that bar was shorter than jj).

3.3. Between those boundaries, all bars are at least as tall as jj (they were pushed after jj and before being popped). So the width calculation (rightleft1)(right - left - 1) is correct.