Bölüm 4 · Simülatör

Stock Span Simulator

← Bölüm 4
Ready.
Monotonic Stack
TOP

Stock Span Simulator

The span of day i is the number of consecutive days up to and including day i whose price is less than or equal to the price on day i.

How to use

  • Type 1–10 positive Prices separated by commas and press Load, or use Example / Random.
  • Next / Previous move one step; Auto Run plays all steps; Reset returns to the start; Cancel stops playback.

Monotonic stack algorithm

  1. For each day i, pop indices while the price on top of the stack is ≤ price[i].
  2. span[i] = i + 1 if the stack is empty, otherwise i − top.
  3. Push i.

The stack holds days with strictly decreasing prices. Every index is pushed and popped at most once, so the whole run is O(n) time (the naive method is O(n²)).