-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path901. Online Stock Span
35 lines (29 loc) · 954 Bytes
/
901. Online Stock Span
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
34
35
class StockSpanner(object):
def __init__(self):
self.list1=[]
self.m=1
self.l=0
self.dict1={}
def next(self, price):
if self.l==0:
self.list1.append(price)
self.l+=1
return self.m
else:
if self.list1[-1]>price:
self.dict1[self.list1[-1]]=self.m
self.m=1
self.list1.append(price)
self.l+=1
return self.m
else:
ans=1
self.dict1[self.list1[-1]]=self.m
while self.l>0 and self.list1[self.l-1]<=price:
ans+=self.dict1[self.list1[self.l-1]]
self.list1.pop()
self.l-=1
self.m=ans
self.list1.append(price)
self.l+=1
return ans