Skip to content

Commit 31b3451

Browse files
authoredNov 9, 2022
Create Online Stock Span.py
1 parent 7786e33 commit 31b3451

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed
 

‎medium/Online Stock Span.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#901. Online Stock Span
2+
class StockSpanner:
3+
def __init__(self):
4+
self.stack = [] # (price, span)
5+
6+
def next(self, price: int) -> int:
7+
span = 1
8+
while self.stack and self.stack[-1][0] <= price:
9+
span += self.stack.pop()[1]
10+
self.stack.append((price, span))
11+
return span

0 commit comments

Comments
 (0)
Please sign in to comment.