← DiffPush

Online Stock Span

Standard Bar Stack and Queues · Implementation O(n) · O(n)

The Market Ticker Retroscope

A trading terminal receives one price per tick and must instantly show the retro-scope count: how many consecutive ticks up to and including now have posted a price at or below today's. Yesterday's ticks beyond the last strictly-higher price are irrelevant — the terminal keeps only those survivors on a shortlist.

Input: An integer array prices given in tick order.

Output: Return the span for each tick: the count of consecutive prior days (including itself) with price <= that day's price.

Constraints

Examples

Example 1

Input: {"prices":[100,80,60,70,60,75,85]}
Output: [1,1,1,2,1,4,6]
75 looks back over 60,70,60 (all <=) and stops at 80; 85 additionally covers the 80, reaching 6.

Example 2

Input: {"prices":[50,50,50]}
Output: [1,2,3]
Equal prices count toward the span, so the run keeps extending.

Solve this in your browser →

Also on LeetCode ↗