901. Online Stock Span

901. Online Stock Span

Problem Solving - Day 39

ยท

3 min read

Hello, reader ๐Ÿ‘‹๐Ÿฝ ! Welcome to day 39 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.

Today, I will be picking up LeetCode's daily challenge problem: 901. Online Stock Span.


๐Ÿค” 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 today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price.
  • Implement the StockSpanner class:

    • StockSpanner() => Initialises the object of the class.
    • int next(int price) => Returns the span of the stock's price given that today's price is price.
  • E.g.:

    • stocks => [100,80,60,70,60,75,85] => [1,1,1,2,1,4,6]

๐Ÿ’ฌ Thought Process - Brute Force

  • The brute force way of doing is this: for every new stock being added, we check starting from the previous stock until the current price is greater.
  • But doing this would cause O(n^2) time in the worst case, which is not efficient
  • Hence, we'll tackle this using a monotonic stack.

๐Ÿ’ฌ Thought Process - Monotonic Stack

  • We can use a monotonic stack that keeps track of values in sorted (descending) order.
  • But how do we compare a current stock with past values? Because we cannot access the previous stocks from the stack since only the top most element can be retrieved.
  • To tackle this, instead of pushing only stock prices, we will push both stock price as well as the stock span.
  • As and when new stocks are added there will be three scenarios:
    1. The stack is empty:
      • Push the stock price and the stock span as 1.
    2. Previous stock price is greater than the current stock price:
      • Push the stock price and stock span as 1.
    3. Previous stock price is greater than or equal to the current stock price:
      • Pop from the stack the top price, add the top stock span to the current stock span count.
      • Repeat this until either the end of stack or the top element becomes greater than the current stock price.

stock_span.png

๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป Solution - Monotonic Stack

class StockSpanner {
    Stack<Pair<Integer, Integer>> max;

    public StockSpanner() {
        max = new Stack<>();
    }

    public int next(int price) {
        int count =  1;

        while(!max.isEmpty() && max.peek().getKey() <= price) {
            count += max.pop().getValue();
        }

        max.add(new Pair(price, count));
        return count;
    }
}

Time Complexity: O(1)

  • n = length of string
    • In the worst case, we will be checking a stock price with all the elements in the stock.
    • But on an average case the TC is O(1) => amortized analysis Space Complexity: O(n)
  • n = length of string
    • We use extra space to maintain stack ```


Conclusion

That's a wrap for today's problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!

Thank you for reading!