leetcode901. Online Stock Span
Posted seyjs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode901. 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
以上是关于leetcode901. Online Stock Span的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 901 股票价格跨度[栈] HERODING的LeetCode之路
LeetCode Best Time to Buy and Sell Stock with Cooldown
[LeetCode]Best Time to Buy and Sell Stock II
LeetCode 121. Best Time to Buy and Sell Stock