901. Online Stock Span
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 67% Topics: Stack, Design, Monotonic Stack, Data Stream
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class StockSpanner(object):
def __init__(self):
self.__s = []
def next(self, price):
"""
:type price: int
:rtype: int
"""
result = 1
while self.__s and self.__s[-1][0] <= price:
result += self.__s.pop()[1]
self.__s.append([price, result])
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions