FunctionRacerProblemsRace

Span of Each Day

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

prices holds a stock's price on consecutive days. The span of day i is the number of consecutive days ending on day i (day i included, counting backwards without a gap) whose price is less than or equal to prices[i].

Return the list of spans, one per day, in day order. The span of the first day is always 1. Return an empty list for an empty prices.

Example 1

prices [100,80,60,70,60,75,85]
returns [1,1,1,2,1,4,6]

Day 3 (price 70) covers itself and the 60 before it; day 6 (price 85) covers all days back to the 80 on day 1 but not the 100 on day 0.

Example 2

prices [1,2,3,4]
returns [1,2,3,4]

Each day is higher than every day before it, so each span reaches back to the start.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems