The stock span problem is a financial problem where we have a series of N daily price quotes for a stock and we need to calculate the span of the stock’s price for all N days. The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
[100 80 60 70 60 75 85] – input
1 1 1 2 1 4 6 – output
This problem asks for the stock span of each day: the number of consecutive previous days whose prices are less than or equal to today’s price. The standard solution uses a monotonic decreasing stack to skip over smaller prices efficiently, allowing all spans to be computed in linear time. For the sample input [100, 80, 60, 70, 60, 75, 85], the output is 1 1 1 2 1 4 6.