Skip to content
We areTOP 1%

Online Stock Span

MediumLeetCodeMonotonic Stack
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Approach hintTry it first, then reveal

Store (price, span) pairs on a stack. When a new price arrives, pop every pair whose price does not exceed it and add their spans to its own before pushing it, so each call is amortised O(1).

More in Monotonic Stack

Monotonic Stack questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Next Greater Element ILeetCodeEasy Solve Next Greater Element I on LeetCode (opens in a new tab)
2Bad PricesCodeforcesEasy 1100Solve Bad Prices on Codeforces (opens in a new tab)
3Minimum NotationCodeforcesEasy 1200Solve Minimum Notation on Codeforces (opens in a new tab)
4Next Greater Element IILeetCodeMedium Solve Next Greater Element II on LeetCode (opens in a new tab)
5Daily TemperaturesLeetCodeMedium Solve Daily Temperatures on LeetCode (opens in a new tab)
6Online Stock SpanLeetCodeMedium Solve Online Stock Span on LeetCode (opens in a new tab)
7Remove K DigitsLeetCodeMedium Solve Remove K Digits on LeetCode (opens in a new tab)
8Largest SubsequenceCodeforcesMedium 1400Solve Largest Subsequence on Codeforces (opens in a new tab)
9Sum of Subarray MinimumsLeetCodeMedium Solve Sum of Subarray Minimums on LeetCode (opens in a new tab)
10Skyscrapers (easy version)CodeforcesMedium 1500Solve Skyscrapers (easy version) on Codeforces (opens in a new tab)
11Largest Rectangle in HistogramLeetCodeHard Solve Largest Rectangle in Histogram on LeetCode (opens in a new tab)
12Maximal RectangleLeetCodeHard Solve Maximal Rectangle on LeetCode (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account