// time O(n)
// space O(n)
class MonotonicStack {
Stack<Integer> prices, weights;
public MonotonicStack() {
prices = new Stack<>();
weights = new Stack<>();
}
public int next(int price) {
int w = 1;
while (!prices.isEmpty() && prices.peek() < price) {
prices.pop();
w += weights.pop();
}
prices.push(price);
weights.push(w);
return w;
}
}
https://leetcode-cn.com/problems/online-stock-span/
思路
[ ] 刷15mins
[ ] 刷5mins