Advertisement

Online Stock Span - LeetCode 901 Solution

Online Stock Span - Complete Solution Guide

Online Stock Span is LeetCode problem 901, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day. The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day. For example, if the prices of the stock in the last four days is [7,2,1,2] and the price of the stock today is 2 , then the span of today is 4 because starting from t

Detailed Explanation

The problem asks us to design a `StockSpanner` class that calculates the 'span' of a stock's price for each day. The span for a given day is defined as the maximum number of consecutive days (including the current day and going backward) where the stock price was less than or equal to the current day's price. We need to implement a `next(int price)` method that takes the current day's price as input and returns its span.

Solution Approach

The solution uses a stack to maintain a decreasing sequence of prices along with their spans. When a new price comes in, we iterate through the stack and pop all the elements whose prices are less than or equal to the current price. As we pop these elements, we accumulate their spans. Finally, we push the current price and its calculated span onto the stack.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack. Each element in the stack will be a pair (price, span).
  2. Step 2: When the `next(price)` method is called, initialize a variable `span` to 1 (the span for the current day itself).
  3. Step 3: While the stack is not empty and the price at the top of the stack is less than or equal to the current `price`, pop the element from the stack and add its span to the current `span`.
  4. Step 4: After popping all the necessary elements, push the current `price` and its calculated `span` onto the stack.
  5. Step 5: Return the calculated `span`.

Key Insights

  • Insight 1: The key insight is that we need to efficiently find the number of consecutive days before the current day where the price was less than or equal to the current day's price.
  • Insight 2: A stack is the appropriate data structure to maintain a decreasing sequence of prices and their spans encountered so far. This allows us to quickly determine the span for the current day.
  • Insight 3: The stack stores pairs of (price, span) for previous days. When a new price comes in, we pop elements from the stack whose price is less than or equal to the current price. The span of the current price is then the sum of the spans of the popped elements plus 1 (for the current day).

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Stack, Design, Monotonic Stack, Data Stream.

Companies

Asked at: INDmoney.