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;
}
}
}
}
单调栈解决股票价格跨度
最新推荐文章于 2023-10-07 22:19:59 发布