bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 901. Online Stock Span

901. Online Stock Span

Leetcode Solutions

Key approach of the solution: Monotonic Stack

  1. Initialize a stack stack to store pairs [price, span] in a monotonically decreasing order.
  2. For each call to next(price), do the following:
    • Initialize span to 1 (the current day).
    • While the stack is not empty and the top element's price is less than or equal to the current price, pop the top element from the stack and add its span to the current span.
    • Push the pair [price, span] onto the stack.
    • Return the current span.
UML Thumbnail

Alternative approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...