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.
[7,2,1,2] and the price of the stock today is 2, then the span of today is 4 because starting from today, the price of the stock was less than or equal 2 for 4 consecutive days.[7,34,1,2] and the price of the stock today is 8, then the span of today is 3 because starting from today, the price of the stock was less than or equal 8 for 3 consecutive days.Implement the StockSpanner class:
StockSpanner() Initializes the object of the class.int next(int price) Returns the span of the stock's price given that today's price is price.
Example 1:
Input ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"] [[], [100], [80], [60], [70], [60], [75], [85]] Output [null, 1, 1, 1, 2, 1, 4, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return 1 stockSpanner.next(80); // return 1 stockSpanner.next(60); // return 1 stockSpanner.next(70); // return 2 stockSpanner.next(60); // return 1 stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price. stockSpanner.next(85); // return 6
Constraints:
1 <= price <= 105104 calls will be made to next.This approach utilizes a stack to track the prices and their corresponding spans. For each new price, we compare it with the top of the stack, which holds the last price and its span. If the new price is higher or equal, we pop from the stack and add the span of the popped element to the current span count. This way, we efficiently calculate the span while ensuring that older prices do not need to be recalculated unnecessarily.
The Python solution uses a list as a stack to store pairs of prices and their spans. For each new price, we initialize the span to 1 and add it to the span of any previous prices from which it is greater or equal, thus ensuring we only process each element once as they are popped from the stack. This makes it efficient for handling up to 10,000 calls.
JavaScript
Time Complexity: O(n), where n is the number of calls to next() because each element is pushed and popped from the stack at most once.
Space Complexity: O(n) for storing the stack.
This approach optimizes memory and simplifies the logic by storing prices along with pre-calculated spans in a tuple or similar structure. This implementation minimizes redundant span computation by leveraging tuple structures efficiently, ideal for platforms that can handle tuple operations swiftly.
The Java solution uses a stack of integer arrays to store prices and their computed spans. When searching for the span for a new price, it checks against the top element of the stack and if the top element's price is less than or equal to the new price, it pops the stack and increases the span accordingly. Finally, it pushes the new price and its span onto the stack.
C#
Time Complexity: O(n), as each element is only processed once.
Space Complexity: O(n), where n is the number of calls, due to stack storage.
| Approach | Complexity |
|---|---|
| Using a Stack to Track Prices and Spans | Time Complexity: O(n), where n is the number of calls to next() because each element is pushed and popped from the stack at most once. |
| Optimized Data Storage with Tuple for Price-Spans | Time Complexity: O(n), as each element is only processed once. |
L15. Stock Span Problem | Stack and Queue Playlist • take U forward • 53,201 views views
Watch 9 more video solutions →Practice Online Stock Span with our built-in code editor and test cases.
Practice on FleetCode