Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of that stock's price for the current day.

The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backwards) for which the price of the stock was less than or equal to today's price.

For example, if the price of a stock over the next 7 days were [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6].

Example 1:

Input: ["StockSpanner","next","next","next","next","next","next","next"], [[],[100],[80],[60],[70],[60],[75],[85]]
Output: [null,1,1,1,2,1,4,6]
Explanation:
First, S = StockSpanner() is initialized. Then:
S.next(100) is called and returns 1,
S.next(80) is called and returns 1,
S.next(60) is called and returns 1,
S.next(70) is called and returns 2,
S.next(60) is called and returns 1,
S.next(75) is called and returns 4,
S.next(85) is called and returns 6. Note that (for example) S.next(75) returned 4, because the last 4 prices
(including today's price of 75) were less than or equal to today's price.

思路:每次插入新值x时,找到前面的比x大的值的位置,那么答案就是当前位置减去那个最大值的位置。 如果没有找到比x大的值,那么就是x的位置减去INT_Max的位置,如下代码实现。

总时间复杂度是O(len(next))

class StockSpanner {
public:
int id = 0;
stack<pair<int, int> > s;
StockSpanner() {
s.push({INT_MAX, 0});
} int next(int price) {
id++;
while (!s.empty() && price >= s.top().first) s.pop();
int begin = s.top().second;
s.push({price, id});
return id - begin;
}
}; /**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/

leetcode 901. Online Stock Span的更多相关文章

  1. [LeetCode] 901. Online Stock Span 股票价格跨度

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  2. [LeetCode] 901. Online Stock Span 线上股票跨度

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  3. LC 901. Online Stock Span

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  4. 【LeetCode】901. Online Stock Span 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leet ...

  5. 【leetcode】901. Online Stock Span

    题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...

  6. 901. Online Stock Span [短于线性的时间统计单个元素的Span ]

    Span 指这个元素之前连续的小于这个元素的值有多少个 原理: 维护递减栈 这个栈内的元素是递减的序列 新到一个元素x 依次出栈比x小的(也就是这个元素的Span) 这种问题的关键在于 新来的元素如果 ...

  7. [Swift]LeetCode901. 股票价格跨度 | Online Stock Span

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. C# 写日志到文件

    C# 写日志到文件 using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms ...

  2. Android下关于消息的推送(9.10)

    1 http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api  百度云推送 2 http://blog.csdn.net/ ...

  3. JQuery加载并解析XML

    转自http://blog.csdn.net/pan_junbiao/article/details/7441003,致谢! 1.简述 XML(eXtensible Markup Language)即 ...

  4. python文件备份脚本

    import osimport time source = ['D:\\MyDrivers\hotfix']   #这里可以用自然字符串表示r',因为windows下的分隔符与python的有冲突,所 ...

  5. 在CentOS 5下安装中文五笔

    由于习惯使用五笔,需要在CentOS5 下安装中文五笔输入法. 刚装好的 CentOS 5默认是没有中文输入 法的.只能显示英文,有中文字符的文件名呈现乱码. 首先挂载CentOS的系统安装盘,在安装 ...

  6. Monkey源代码分析之事件源

    上一篇文章<Monkey源代码分析之执行流程>给出了monkey执行的整个流程.让我们有一个概貌,那么往后的文章我们会尝试进一步的阐述相关的一些知识点. 这里先把整个monkey类的结构图 ...

  7. 机器学习13—PCA学习笔记

     主成分分析PCA 机器学习实战之PCA test13.py #-*- coding:utf-8 import sys sys.path.append("pca.py") impo ...

  8. java.io.IOException: Illegal partition for 67 (-1)

    今天写MapReduce的分区进行排序的功能,自己写了一个Partitioner,然后用的时候就错了 public static class MyPartition extends Partition ...

  9. 在Mac上为自己手动编译安装一套PHP7的开发环境

    首先你得去官网下载php7 beta1的版本 这里由于我是在mac上安装,所以就去下载linux相关的版本,地址也直接附上了php7 beta1windows版的官方也有发布详情猛戳:这里 解压安装包 ...

  10. 怎样解决mysql最后一步提示未响应

    1.在开始菜单下,点击运行,输入regedit,进入注册表编辑器目录下 2.在注册表编辑器里system下找到controlset001,controlset002,currentcontrolset ...