【leetcode】901. Online Stock Span

题目如下:

解题思路:【leetcode】84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素。

代码如下:

class StockSpanner(object):
    def __init__(self):
        self.cl = []
        self.vl = []

    def next(self, price):
        """
        :type price: int
        :rtype: int
        """
        count = 1
        if len(self.vl) == 0:
            self.vl.append(price)
            self.cl.append(1)
        else:
            startInx = len(self.vl) - 1
            while startInx >= 0 and price >= self.vl[startInx]:
                count += self.cl[startInx]
                startInx -= self.cl[startInx]
            self.vl.append(price)
            self.cl.append(count)
        return count

 

posted @ 2018-09-17 17:50  seyjs  阅读(313)  评论(0编辑  收藏  举报