码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】901. Online Stock Span

时间:2018-09-17 19:45:01      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:.com   end   init   alt   tps   div   htm   else   art   

题目如下:

技术分享图片

解题思路:【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

 

【leetcode】901. Online Stock Span

标签:.com   end   init   alt   tps   div   htm   else   art   

原文地址:https://www.cnblogs.com/seyjs/p/9613320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!