blob: 9cd8c3a711369ec6c61e89c0d6055bf834d7d685 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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))
|