Skip to content
LC-0901 Medium LeetCode

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
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