[LeetCode] 901. Online Stock Span 线上股票跨度
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:
- Calls to
StockSpanner.next(int price)will have1 <= price <= 10^5. - There will be at most
10000calls toStockSpanner.nextper test case. - There will be at most
150000calls toStockSpanner.nextacross all test cases. - 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 线上股票跨度的更多相关文章
- [LeetCode] 901. Online Stock Span 股票价格跨度
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...
- leetcode 901. Online Stock Span
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...
- LC 901. Online Stock Span
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...
- 【LeetCode】901. Online Stock Span 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leet ...
- 【leetcode】901. Online Stock Span
题目如下: 解题思路:和[leetcode]84. Largest Rectangle in Histogram的核心是一样的,都是要找出当前元素之前第一个大于自己的元素. 代码如下: class S ...
- 901. Online Stock Span [短于线性的时间统计单个元素的Span ]
Span 指这个元素之前连续的小于这个元素的值有多少个 原理: 维护递减栈 这个栈内的元素是递减的序列 新到一个元素x 依次出栈比x小的(也就是这个元素的Span) 这种问题的关键在于 新来的元素如果 ...
- [Swift]LeetCode901. 股票价格跨度 | Online Stock Span
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...
- httpclient+jsoup实现小说线上采集阅读
前言 用过老版本UC看小说的同学都知道,当年版权问题比较松懈,我们可以再UC搜索不同来源的小说,并且阅读,那么它是怎么做的呢?下面让我们自己实现一个小说线上采集阅读.(说明:仅用于技术学习.研究) 看 ...
- 我整理的一份来自于线上的Nginx配置(Nginx.conf),希望对学习Nginx的有帮助
我整理了一份Nginx的配置文件说明,是真正经历过正式线上考验过.如果有优化的地方,也请朋友们指点一二,整理出一份比较全而实用的配置. 主要包含配置:负载均衡配置,页面重定向,转发,HTTPS和HTT ...
随机推荐
- python应用-输出验证码
from random import randint def generate_code (code_len): """ 生成确定位数的验证码 :param code_l ...
- MapTask工作机制
(1)Read阶段:MapTask通过用户编写的RecordReader,从输入InputSplit中解析出一个个key/value. (2)Map阶段:该节点主要是将解析出的key/value交给用 ...
- 【坑】js语法中一些小细节 不注意也出坑 随笔记下 留待后查
1.switch case内 区分数字 与 字符 ',bl; switch(+lv){ :bl = 1.7;break; :bl = 1.55;break; :bl = 1.4;break; ; } ...
- eslint Cannot read property 'range' of null错误
eslint Cannot read property 'range' of null错误 手动添加的配置,2个项目OK,还个项目 运行报错 Cannot read property 'range ...
- 将两个各有n个元素的有序表归并成一个有序表,其最多的比较次数
最多的比较次数是当两个有序表的数据刚好是插空顺序的时候,比如:第一个序列是1,3,5,第二个序列是2,4,6,把第二个序列插入到第一个序列中,先把第二个序列中的第一个元素2和第一个序列依次比较,需要比 ...
- 50: Luogu P4568 分层图
分层图最短路模板 #include <iostream> #include <cstdio> #include <cstdlib> #include <cti ...
- exlucas易错反思
模板和题解 复习了一下 exlucas的模板,结果写挂四次(都没脸说自己以前写过 是该好好反思一下呢~ 错的原因如下: 第一次WA:求阶乘的时候忘了递归处理(n/p)! 第二次WA:求阶乘时把p当成循 ...
- 洛谷 P5614题解
吐槽:数据好像有点水,直接枚举到200可以得80 points. 另:我还是太弱了,比赛的时候只有90 points,#7死卡不过去,最后发现是没有判断 \(z_1\) 和 \(z_2\) 的范围-- ...
- oracle中union和union all区别与性能分析
[ 概要 ] 经常写sql的同学可能会用到union和union all这两个关键词, 可能你知道使用它们可以将两个查询的结果集进行合并, 那么二者有什么区别呢? 下面我们就简单的分析下. [ 比较 ...
- 小程序组件--> 组件传参
小程序组件,在components文件夹右击-->创建文件夹-->右击-->新建component即可 创建一个组件 如果多个地方需要使用到,可以在app.json中加入一下代码,相 ...