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;
}
}
}
}
Monotonic stack solves stock price span
Guess you like
Origin blog.csdn.net/changbaishannefu/article/details/115257447
Recommended
Ranking