You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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);
*/
这道题也是单调栈的解法。
单调栈看上去很简单,也就是栈内结构有序,但可以通过存储数据结构的多变来实现多种功能。
参考资料:
The text was updated successfully, but these errors were encountered: