-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
这道题也是单调栈的解法。
单调栈看上去很简单,也就是栈内结构有序,但可以通过存储数据结构的多变来实现多种功能。
class StockSpanner {
Deque<int[]> stack;
public StockSpanner() {
stack = new LinkedList<>();
}
public int next(int price) {
int cnt = 1;
while (!stack.isEmpty() && price >= stack.peek()[0]) {
cnt += stack.pop()[1];
}
stack.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);
*/
参考资料:
Metadata
Metadata
Assignees
Labels
No labels