diff options
author | Jack Sangdahl <[email protected]> | 2024-08-07 17:17:12 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-08-07 17:17:12 -0600 |
commit | e8d76d8e669d833541f1b6a2923fb80fca2e8407 (patch) | |
tree | 3262dc7664969e7ec395c38925af388258cfd414 /online_stock_span.py | |
parent | fec032985abdf5c864ab35493678346f186f3630 (diff) | |
download | leetcode-e8d76d8e669d833541f1b6a2923fb80fca2e8407.tar.gz leetcode-e8d76d8e669d833541f1b6a2923fb80fca2e8407.zip |
901. online stock span
Diffstat (limited to 'online_stock_span.py')
-rw-r--r-- | online_stock_span.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/online_stock_span.py b/online_stock_span.py new file mode 100644 index 0000000..9cd8c3a --- /dev/null +++ b/online_stock_span.py @@ -0,0 +1,33 @@ +# 901. Online Stock Span + +""" +design an algorithm that collects daily price quotes for some stock and +from that day and going backward) for which the stock price was less than or +equal to the price of that day. if the prices of the stock in the last four +days is '[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. +""" + + +class StockSpanner: + def __init__(self): + self.stack = [] + + def next(self, price: int) -> int: + ans = 1 + while self.stack and self.stack[-1][0] <= price: + ans += self.stack.pop()[1] + self.stack.append([price, ans]) + return ans + + +if __name__ == "__main__": + obj = StockSpanner() + print(obj.next(100)) + print(obj.next(80)) + print(obj.next(60)) + print(obj.next(70)) + print(obj.next(60)) + print(obj.next(75)) + print(obj.next(85)) |