[
stack
monotonic deque
design
]
BinarySearch 0929 Stock Span
Problem statement
https://binarysearch.com/problems/Stock-Span/
Solution
Equal to Leetcode 0901 Online Stock Span.
Complexity
Time and space complexity is O(n)
after n
operations made.
Code
class StockSpan:
def __init__(self):
self.stack = [(10**9, 1)]
def next(self, price):
if price < self.stack[-1][0]:
self.stack.append([price, 1])
else:
count = 1
while price >= self.stack[-1][0]:
count += self.stack[-1][1]
self.stack.pop()
self.stack.append([price, count])
return self.stack[-1][1]