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. 让UITableView进入编辑模式

    1.UITableView对象有一个editing属性,设为YES时,该对象会进入编辑模式(editing mode).表格视图进入编辑模式后,用户可以管理表格中得行,如改变行的排列顺序.增加行或删除 ...

  2. Python---copy()、deepcopy()与赋值的区别

    copy()与deepcopy()之间的主要区别是python对数据的存储方式. 首先直接上结论: —–深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在.所以改变原有被复制对象不会对已经复 ...

  3. 【已解决】WebUploader 0.1.5 安卓手机不能访问相机、IOS直接访问相机 的问题

    WebUploader 0.1.5 安卓手机不能访问相机.IOS直接访问相机 的问题 打开 webuploader.js if(navigator.userAgent.indexOf('Android ...

  4. 【Hadoop基础教程】3、Hadoop之伪分布式环境搭建(转)

    伪分布式模式即单节点集群模式,所有的守护进程都运行在同一台机器上.这种模式下增加了代码调试功能,可以查看内存.HDFS文件系统的输入/输出,以及与其他守护进程交互.以hadoop用户远程登录K-Mas ...

  5. DuiVision开发教程(18)-弹出窗

    DuiVision的弹出窗体类CDlgPopup,是菜单.下拉列表等控件的父类,也能够单独使用,用于创建弹出窗体.弹出窗体默认是非激活状态下自己主动关闭,比如鼠标点击到弹出窗体外面的区域,弹出窗体就会 ...

  6. Ubuntu14下Hadoop开发&lt;1&gt; 基础环境安装

    准备了一台淘汰的笔记本.单核CPU.3G内存.160G硬盘:准备一个2G的U盘 在官网下载了64位的14.04版本号(麒麟)的ISO.下载UNetbootin(Ubuntu专用U盘安装工具) 使用UN ...

  7. 使用Percona监控插件监控MySQL

    1.使用Percona监控插件监控MySQL yum install http://www.percona.com/downloads/percona-release/redhat/0.1-3/per ...

  8. IntelliJ idea——》创建tag、删除tag

    https://blog.csdn.net/weixin_43453386/article/details/83857038

  9. hibernate 注解之 SequenceGenerator

    hibernate 注解之 SequenceGenerator https://blog.csdn.net/zgf19930504/article/details/54694807 JPA @Id 和 ...

  10. 08 nginx Location总结图解