LeetCode 901 - Online Stock Span
Difficulty: medium
Problem Description
English (Online Stock Span)
Design an algorithm that 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 in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.
- For example, if the prices of the stock in the last four days is
[7,2,1,2]
and the price of the stock today is2
, then the span of today is 4 because starting from today, the price of the stock was less than or equal2
for4
consecutive days. - Also, if the prices of the stock in the last four days is
[7,34,1,2]
and the price of the stock today is8
, then the span of today is3
because starting from today, the price of the stock was less than or equal8
for3
consecutive days.
Implement the StockSpanner
class:
StockSpanner()
Initializes the object of the class.int next(int price)
Returns the span of the stock’s price given that today’s price isprice
.
Example 1:
1 |
|
Constraints:
1 <= price <= 10^5
- At most
10^4
calls will be made tonext
.
Chinese (股票价格跨度)
设计一个算法收集某些股票的每日报价,并返回该股票当日价格的 跨度 。
当日股票价格的 跨度 被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今天)。
例如,如果未来 7 天股票的价格是 [100,80,60,70,60,75,85]
,那么股票跨度将是 [1,1,1,2,1,4,6]
。
实现 StockSpanner
类:
StockSpanner()
初始化类对象。int next(int price)
给出今天的股价 price
,返回该股票当日价格的 跨度 。
示例:
1 |
|
提示:
1 <= price <= 10^5
- 最多调用
next
方法10^4
次
Solution
C++
1 |
|
LeetCode 901 - Online Stock Span
http://wasprime.github.io/Algorithm/LeetCode/LeetCode-901-Online-Stock-Span/