2Brute2Best
πŸ”₯0

Online Stock Span

MediumΒ·LeetCode #901 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

Build a class StockSpanner that receives a stock's price one day at a time.

Each call next(price) gives you today's price and must return today's span: the number of days in a row, ending today and going backwards, on which the price was less than or equal to today's price. Today itself counts, so the span is at least 1.

You do not get the prices in advance β€” you must answer each call before you see the next price.

Input

A sequence of calls: StockSpanner() once, then next(price) many times.

Output

For each next(price) call, an integer: today's span.

Example 1
Input: StockSpanner(), next(100), next(80), next(60), next(70), next(60), next(75), next(85)
Output: null, 1, 1, 1, 2, 1, 4, 6

For 75: going back, 60, 70, 60 are all ≀ 75, and 80 is higher, so the span is 4. For 85: everything back to 100 is ≀ 85, so the span is 6.

Constraints
  • β€’ 1 ≀ price ≀ 10^5
  • β€’ At most 10^4 calls to `next`
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œone day at a time”
β†’ An online stream: you can't look ahead, and you don't want to store and rescan the whole history.
β€œdays in a row … less than or equal to today”
β†’ Same boundary as the array version: stop at the previous strictly greater price.
β€œreturn the span on each call”
β†’ Each call must be fast on average β†’ monotonic stack whose entries carry their span.
03

Approach Complexity Ladder

Brute Force β€” keep every price, walk back each callO(N)O(N)

Store all prices; each call counts backwards while prices are ≀ today

Optimal β€” stack of (price, span) pairsO(1)O(N)

Each stack entry stands for a whole run of days; popped spans fold into the new one

Brute Force Intuition

Keep every price in a vector. On each next(price), append it, then walk back from the second-last price while prices are ≀ price, counting as you go.

Recognition rule for this sub-pattern:

"How many consecutive previous items are ≀ me?" Popped items fold their span into the new one.
Remember this Β· Monotonic Stack
Trigger
Next/previous greater or smaller element, spans, histogram areas, "for each element, how far does it dominate?"
Think
When a new element arrives, which older elements can never be the answer again?
Key question
Is my stack increasing or decreasing, and what do I record when I pop?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.