💡
Seb's Whiteboard
  • 👨‍💻Welcome, I'm Sebastien St Vil
  • Extras
    • Gradient Descent
    • How I learned Java
    • Machine Learning by Andrew Ng
  • Projects
    • 📉Backtest Equity Trading with SMA Strategy
    • Wellington GA Lab
    • Stock analysis
    • 📈Time Series Regression-based Trading Strategy
  • Arrays & Strings
    • Best Time to Buy and Sell Stock II
    • Online Stock Span
    • Implement strStr()
    • 2Sum
    • 3Sum
    • 3Sum Closest
    • 4Sum II
    • Set Matrix Zeroes
    • Group Anagrams
    • Longest Substring Without Repeating Characters
    • Remove Duplicates from Sorted Array
    • Move Zeroes
    • Valid Sudoku
    • Rotate Image
    • First Unique Character in a String
    • Design a Circular Queue
    • Longest Common Prefix
  • Binary Tree
  • Second Minimum Node In a Binary Tree (671)
  • Design
  • LRU Cache
  • Min Stack (155)
  • Sorting & Searching
    • Merge Sorted Array (88)
    • First Bad Version
  • Math
    • Power of Three (326)
    • Power of Two (231)
    • Count Prime (204)
    • Roman to Integer (13)
    • Fizz Buzz (412)
    • Count-and-Say
  • Dynamic Programming
    • Pascal's Triangle (118)
  • Linked List
    • Copy List with Random Pointer
    • Remove Nth Node From End of List
    • Remove Duplicated from Sorted List II
  • Tips
    • Finding dups
  • Sliding Window
    • Subarray Product Less Than K
Powered by GitBook
On this page

Was this helpful?

  1. Arrays & Strings

Online Stock Span

Write a class StockSpanner which 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 backwards) for which the price of the stock was less than or equal to today's price.

For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].

Approach: Monotonic Stack

To tackle this problem, we'll again make the use of the monotonic Stack.

A monotonic stack keeps stack elements always ordered (either increasing or decreasing ). As it relates to our algorithm, price span will track the span of smaller elements as it is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.

Therefore, we choose monotonic stack with decreasing order as data structure to support our implementation.

Time Complexity: O(N) ---> we'll iterate over the array once

Space Complexity: O(N) ---> Ex: Let's consider that the stock's quote ever decreases, in the worst case, the stack will contain all the daily price quotes. [100, 80, 60, 50, 40, 35]

Class StockSpanner:

     def __init__(self):
        self.stack = collections.deque()

     def next(self, price: int)-> int:
        span = 1
        while self.stack and price >= self.stack[-1].price_quote:
            span += self.stack.pop().price_span

        self.stack.append(Stock(price, span))

        return span
 
Class Stock:
    def __init__(self, price_quote, price_span):
        self.price_quote = price_quote
        self.price_span = price_span
PreviousBest Time to Buy and Sell Stock IINextImplement strStr()

Last updated 3 years ago

Was this helpful?