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.

Note:

  1. Calls to StockSpanner.next(int price) will have 1 <= price <= 10^5.
  2. There will be at most 10000 calls to StockSpanner.next per test case.
  3. There will be at most 150000 calls to StockSpanner.next across all test cases.
  4. The total time limit for this problem has been reduced by 75% for C++, and 50% for all other languages.
 

Runtime: 225 ms, faster than 32.65% of Java online submissions for Online Stock Span.

class StockSpanner {
private Stack<Integer> s;
private List<Integer> alist;
private int cnt;
public StockSpanner() {
s = new Stack<>();
alist = new ArrayList<>();
cnt = ;
} public int next(int price) {
while( !s.empty() && alist.get(s.peek()) <= price ){
s.pop();
}
int tmpval;
if(!s.empty()) tmpval = s.peek();
else tmpval = -;
s.add(cnt);
alist.add(price);
cnt++;
return s.peek() - tmpval;
}
}

another solution

class StockSpanner {
List<Stock> stocks = new ArrayList<>();
public StockSpanner() {
// prices = new ArrayList<>();
// counts = new ArrayList<>();
} public int next(int price) { // Need to search backwards for the next highest price
int count = ;
// int countIndex = size-1; // int countIndex = size-1;
while(stocks.size() >= count) {
Stock s = stocks.get(stocks.size() - count);
if (s.price > price) {
break;
} else {
count += s.count;
}
}
stocks.add(new Stock(price, count));
// counts.add(count);
return count;
} class Stock {
int price;
int count; public Stock(final int price, final int count) {
this.price = price;
this.count = count;
}
} }

LC 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. [LeetCode] 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. 901. Online Stock Span [短于线性的时间统计单个元素的Span ]

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

  6. 【leetcode】901. Online Stock Span

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

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

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

  8. 单调队列 Monotonic Queue / 单调栈 Monotonic Stack

    2018-11-16 22:45:48 一.单调队列 Monotone Queue 239. Sliding Window Maximum 问题描述: 问题求解: 本题是一个经典的可以使用双端队列或者 ...

  9. 算法与数据结构基础 - 堆栈(Stack)

    堆栈基础 堆栈(stack)具有“后进先出”的特性,利用这个特性我们可以用堆栈来解决这样一类问题:后续的输入会影响到前面的阶段性结果.线性地遍历输入并用stack处理,这类问题较简单,求解时间复杂度一 ...

随机推荐

  1. SSISDB8:查看SSISDB记录Package执行的消息

    在执行Package时,SSISDB都会创建唯一的OperationID 和 ExecutionID,标识对package执行的操作和执行实例(Execution Instance),并记录opera ...

  2. VM安装vmtools后centos7无法上网

    先安装VmTools工具 文件 /etc/sysconfig/network-scripts/ifcfg-ens33(这里的enp0s3不是固定的,看你具体情况,但是基本是en开头的) 将 ONBOO ...

  3. 第四章、前端之BOM和DOM

    目录 第四章.前端之BOM和DOM 一.解释BOM和DOM 二.window对象 三.window子对象 四.弹出框 五.计时相关 六.HTML的DOM树 七.查找元素 八.节点操作 九.JS操作CS ...

  4. ie11浏览器不显示vbs脚本

    最初接触学习vbs在浏览器上运行,老不显示vbscript脚本语言,所以找了很久,最后就用这个方法吧,比较简单有效 原因:新版IE不再支持 VBScript,就是因为微软已经放弃把VBScript作为 ...

  5. 2.Bacula Server端安装配置

    1.  Bacula Server端安装配置 1.1.  Bacula Server端安装 1.1.1.  安装bacula依赖包 For Centos6: yum install -y mysql ...

  6. 01_Hive简介及其工作机制

    1.Hive简介 Hive是一个基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一个表.并提供类SQL查询功能, 可以将sql语句转换为MapReduce任务运行.其优点是学习成本低, ...

  7. nodejs建站+github page 建站问题总结

    本文介绍 昨天吃晚饭的时候,在B站偶然看到一个关于搭建自己博客的视频,过程讲的很详细,于是就有了自己想尝试一下的冲动,所以,在晚上的时候,尝试了下,但是,过程并没有视频中说的那么顺利,看了网上很多帖子 ...

  8. oracle 初试 hint

    最近在研究oracle的视图问题,本来想全转成 物化视图(materialized view)的,这样可以极大提升系统的响应时间,无奈工作量太大,所以就研究了SQL优化的问题. 我这个普通视图 有36 ...

  9. jQuery超酷响应式瀑布流效果

    参考 http://www.sucaihuo.com/js/74.html <script src="scripts/blocksit.min.js"></scr ...

  10. python 前置程序窗口,还原最小化的窗口

    python 前置程序窗口,还原最小化的窗口 在网上找了比较久,大多是: win32gui.FindWindow(class_name, window_name) win32gui.SetForegr ...