首页 > 其他 > 详细

【leetcode】901. Online Stock Span

时间:2018-09-17 19:06:50      阅读:253      评论:0      收藏:0      [点我收藏+]

题目如下:

技术分享图片

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!