-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path901.py
More file actions
28 lines (19 loc) · 659 Bytes
/
901.py
File metadata and controls
28 lines (19 loc) · 659 Bytes
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
class StockSpanner:
def __init__(self):
self.stack = []
self.prices = []
self.index = 0
def next(self, price: int) -> int:
self.prices.append(price)
# index = 2
# prices = [100, 80, 60, 70]
# stack = [0, 1, 2]
while self.stack and self.prices[self.stack[-1]] <= price:
self.stack.pop()
res = self.index + 1 if not self.stack else self.index - self.stack[-1]
self.stack.append(self.index)
self.index += 1
return res
# Your StockSpanner object will be instantiated and called as such:
# obj = StockSpanner()
# param_1 = obj.next(price)