[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 ...
随机推荐
- Game Based Learning: Why Does it Work?
Forty years of research[i] says yes, games are effective learning tools. People learn from games, an ...
- Servlet 容器
Servlet容器主要是JavaWeb应用提供运行时环境,所以也可以称之为JavaWeb应用容器,或者Servlet/JSP容器.Servlet容器主要负责管理Servlet.JSP的生命周期以及它们 ...
- spring是什么?
spring是什么? 1.编程范式的实践 dsl.注解.aop技术,扩展java语言的表达能力: dsl:xml配置+注解配置,扩展工程的组织能力: 2.基础组件: 常用组件的便捷封装,方便进行二次开 ...
- uiwebview 离线缓存 图片
uiwebview 离线缓存图片
- (尚033)Vue_案例_slot(组件间的通信4:slot)
1.组件间的通信4:slot(slot:插槽,就是一个占位) slot用于标签反复使用很多次 1.1理解 此方式用于父组件向子组件传递标签数据, 其他为数据通信 外面组件向里面组件传递标签进去,直接拿 ...
- ent 基本使用十八 查询谓词
ent 生成的代码包含了比较完整的查询谓词 字段谓词 Bool: =, != Numeric: =, !=, >, <, >=, <=, IN, NOT IN Time: =, ...
- 鸿蒙OS
8月9日,华为消费者业务在其全球开发者大会上正式发布其全新的基于微内核的面向全场景的分布式操作系统——鸿蒙OS(HarmonyOS)! 鸿蒙的定义是基于微内核的全场景分布式操作系统.其中,微内核是技术 ...
- python:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
# 将默认编码设为utf-8 # 否则会报错: # UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ...
- WPF——OXY绘图_old
plotModel = new PlotModel() { Title = "数据统计", LegendTitle = "Max:红色,Min:黄色", Leg ...
- BIND配置
一,简介 相对于存储和大数据领域,CDN是一个相对小的领域,但行行出状元,BIND就是CDN领域的蝉联N届的状元郎.BIND是一款非常常用的DNS开源服务器,全球有90%的DNS用BIND实现.值得一 ...