Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.

Sign in to watch this lesson
Algorithms - Linear Data Structures · 4:28
A stack that stays rising from bottom to top answers "nearest smaller to the left" for every element in one linear pass: each new bar knocks off the bars at least as tall, and whatever is left on top is its answer. We trace it on [3, 7, 8, 4, 2, 6, 5], cover the four variants and the tie rules, then let the same stack hand every bar of the histogram [2, 1, 5, 6, 2, 3] both of its boundaries to find the largest rectangle (10), and finish with the contribution technique for the sum of subarray minimums. Timings on this Mac compare the stack with the quadratic scans.