题目如下:
解题思路:和【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