comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
中等 |
|
设计一个算法收集某些股票的每日报价,并返回该股票当日价格的 跨度 。
当日股票价格的 跨度 被定义为股票价格小于或等于今天价格的最大连续日数(从今天开始往回数,包括今天)。
-
例如,如果未来 7 天股票的价格是
[100,80,60,70,60,75,85]
,那么股票跨度将是[1,1,1,2,1,4,6]
。
实现 StockSpanner
类:
StockSpanner()
初始化类对象。int next(int price)
给出今天的股价price
,返回该股票当日价格的 跨度 。
示例:
输入: ["StockSpanner", "next", "next", "next", "next", "next", "next", "next"] [[], [100], [80], [60], [70], [60], [75], [85]] 输出: [null, 1, 1, 1, 2, 1, 4, 6] 解释: StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // 返回 1 stockSpanner.next(80); // 返回 1 stockSpanner.next(60); // 返回 1 stockSpanner.next(70); // 返回 2 stockSpanner.next(60); // 返回 1 stockSpanner.next(75); // 返回 4 ,因为截至今天的最后 4 个股价 (包括今天的股价 75) 都小于或等于今天的股价。 stockSpanner.next(85); // 返回 6
提示:
1 <= price <= 105
- 最多调用
next
方法104
次
根据题目描述,我们可以知道,对于当日价格
这实际上是经典的单调栈模型,找出左侧第一个比当前元素大的元素。
我们维护一个从栈底到栈顶价格单调递减的栈,栈中每个元素存放的是
出现价格
最后将
时间复杂度 next(price)
的次数。
class StockSpanner:
def __init__(self):
self.stk = []
def next(self, price: int) -> int:
cnt = 1
while self.stk and self.stk[-1][0] <= price:
cnt += self.stk.pop()[1]
self.stk.append((price, cnt))
return cnt
# Your StockSpanner object will be instantiated and called as such:
# obj = StockSpanner()
# param_1 = obj.next(price)
class StockSpanner {
private Deque<int[]> stk = new ArrayDeque<>();
public StockSpanner() {
}
public int next(int price) {
int cnt = 1;
while (!stk.isEmpty() && stk.peek()[0] <= price) {
cnt += stk.pop()[1];
}
stk.push(new int[] {price, cnt});
return cnt;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/
class StockSpanner {
public:
StockSpanner() {
}
int next(int price) {
int cnt = 1;
while (!stk.empty() && stk.top().first <= price) {
cnt += stk.top().second;
stk.pop();
}
stk.emplace(price, cnt);
return cnt;
}
private:
stack<pair<int, int>> stk;
};
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner* obj = new StockSpanner();
* int param_1 = obj->next(price);
*/
type StockSpanner struct {
stk []pair
}
func Constructor() StockSpanner {
return StockSpanner{[]pair{}}
}
func (this *StockSpanner) Next(price int) int {
cnt := 1
for len(this.stk) > 0 && this.stk[len(this.stk)-1].price <= price {
cnt += this.stk[len(this.stk)-1].cnt
this.stk = this.stk[:len(this.stk)-1]
}
this.stk = append(this.stk, pair{price, cnt})
return cnt
}
type pair struct{ price, cnt int }
/**
* Your StockSpanner object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Next(price);
*/
class StockSpanner {
private stk: number[][];
constructor() {
this.stk = [];
}
next(price: number): number {
let cnt = 1;
while (this.stk.length && this.stk.at(-1)[0] <= price) {
cnt += this.stk.pop()[1];
}
this.stk.push([price, cnt]);
return cnt;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* var obj = new StockSpanner()
* var param_1 = obj.next(price)
*/
use std::collections::VecDeque;
struct StockSpanner {
stk: VecDeque<(i32, i32)>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl StockSpanner {
fn new() -> Self {
Self {
stk: vec![(i32::MAX, -1)].into_iter().collect(),
}
}
fn next(&mut self, price: i32) -> i32 {
let mut cnt = 1;
while self.stk.back().unwrap().0 <= price {
cnt += self.stk.pop_back().unwrap().1;
}
self.stk.push_back((price, cnt));
cnt
}
}