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.

给一个股票价格的数组,写一个函数计算每一天的股票跨度,股票跨度是从当天股票价格开始回看连续的比当天价格低的最大连续天数。

解法1:简单但效率低的思路,对于每一天向后计算连续比当前价格低的天数。时间复杂度:O(n^2)

解法2: 栈

You can refer to the same problem 739. Daily Temperatures.

Push every pair of <price, result> to a stack.
Pop lower price from the stack and accumulate the count.

One price will be pushed once and popped once.
So 2 * N times stack operations and N times calls.
I'll say time complexity is O(1)

Java:

Stack<int[]> stack = new Stack<>();
public int next(int price) {
int res = 1;
while (!stack.isEmpty() && stack.peek()[0] <= price)
res += stack.pop()[1];
stack.push(new int[]{price, res});
return res;
}  

Python:

def __init__(self):
self.stack = [] def next(self, price):
res = 1
while self.stack and self.stack[-1][0] <= price:
res += self.stack.pop()[1]
self.stack.append([price, res])
return res

C++:

stack<pair<int, int>> s;
int next(int price) {
int res = 1;
while (!s.empty() && s.top().first <= price) {
res += s.top().second;
s.pop();
}
s.push({price, res});
return res;
}

  

类似题目:  

[LeetCode] 739. Daily Temperatures 每日温度

All LeetCode Questions List 题目汇总

[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. httpclient+jsoup实现小说线上采集阅读

    前言 用过老版本UC看小说的同学都知道,当年版权问题比较松懈,我们可以再UC搜索不同来源的小说,并且阅读,那么它是怎么做的呢?下面让我们自己实现一个小说线上采集阅读.(说明:仅用于技术学习.研究) 看 ...

  9. 我整理的一份来自于线上的Nginx配置(Nginx.conf),希望对学习Nginx的有帮助

    我整理了一份Nginx的配置文件说明,是真正经历过正式线上考验过.如果有优化的地方,也请朋友们指点一二,整理出一份比较全而实用的配置. 主要包含配置:负载均衡配置,页面重定向,转发,HTTPS和HTT ...

随机推荐

  1. 关于Java锁(学习笔记)

    个人学习笔记! 1)分布式锁的实现?①数据库实现单点.非重入.非阻塞.无失效时间.依赖数据库(要自己设置,可结合排它锁.乐观锁.悲观锁等混合使用)②缓存(Redis等)集群部署解决单点问题.分布式锁方 ...

  2. UI系统综述:iOS的图形绘制、动画与runloop

    一.一条业务pipeline: 一个连接核心:coreanimation 二.两个进程: 1.app进程: 2.render进程: 首先,由 app 处理事件(Handle Events),如:用户的 ...

  3. 在Maven项目中,jsp不解析el表达式

    我的这个项目是用Maven-archetype-webapp项目创建的,如下图所示: 有这种方式创建有一个坑,就是它使用的servlet版本是2.3,而servlet2.4以下的版本是不会自动解析el ...

  4. Python 鼠标键盘操作

    1.鼠标操作 from pymouse import PyMouse myMouse = PyMouse() #获取当前的鼠标位置 # nowP = myMouse.position() # prin ...

  5. 2019 CSP-J复赛游记

    不出行?不出行考屁呢? 今天的CSP-J似乎比去年简单了一些,可它... 好了,来说一说我的情况. T1:太水,5分钟秒 T2:这个数据有点尴尬,双重循环铁定爆,用链表有有一点小题大做.本蒟蒻在考场上 ...

  6. BZOJ 3561: DZY Loves Math VI 莫比乌斯反演+复杂度分析

    推到了一个推不下去的形式,然后就不会了 ~ 看题解后傻了:我推的是对的,推不下去是因为不需要再推了. 复杂度看似很大,但其实是均摊 $O(n)$ 的,看来分析复杂度也是一个能力啊 ~ code: #i ...

  7. 利用伪寄存器对MSVC++进行调试的介绍

    简介 让我们从我写这篇文章的原因开始.一天,一个同事让我帮他调试他遇到的问题.所以我看着他在输入代码,这时我注意到下面一行: int test = GetLastError(); 他这样做是因为他想知 ...

  8. podium layout 说明

    layout 主要是进行podlets 的组合,同时也提供了context ,fallback,以及传递参数的处理 基本代码 const express = require('express'); c ...

  9. 如何安全地使用redis的pop命令

    Redis的list经常被当作队列使用,左进右出,一般生产者使用lpush压入数据,消费者调用rpop取出数据. 这是很自然的行为,然而有时会发现lpush成功,但rpop并没有取到数据,特别是一些客 ...

  10. luogu 2742 二维凸包

    链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...