1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class StockSpanner {
private final Stack<int[]> decreaseStack; private int index;
public StockSpanner() { this.decreaseStack = new Stack<>(); this.index = -1;
decreaseStack.push(new int[]{index, Integer.MAX_VALUE}); }
public int next(int price) { index++;
while (decreaseStack.peek()[1] <= price) { decreaseStack.pop(); }
int peekIndex = decreaseStack.peek()[0]; decreaseStack.push(new int[]{index, price}); return index - peekIndex; }
}
|