class StockSpanner {
Stack<Integer> stack1,stack2;
public StockSpanner() {
stack1=new Stack<>();
stack2=new Stack<>();
}
public int next(int price) {
int t=1;
if(stack1.empty()){
stack1.push(price);
stack2.push(1);
return 1;
}else {
if(stack1.peek()>price){
stack1.push(price);
stack2.push(1);
return 1;
}else{
while(!stack1.empty()&&stack1.peek()<=price){
stack1.pop();
t+=stack2.pop();
}
stack1.push(price);
stack2.push(t);
return t;
}
}
}
}
单调栈解决股票价格跨度
最新推荐文章于 2024-05-04 23:59:19 发布