-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockSpanner.java
More file actions
43 lines (33 loc) · 785 Bytes
/
Copy pathStockSpanner.java
File metadata and controls
43 lines (33 loc) · 785 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package stack;
import java.util.Stack;
/**
* 单调栈
*/
class StockSpanner {
static class Stock{
int price;
int span;
public Stock(int price,int span){
this.price = price;
this.span = span;
}
}
Stack<Stock> stock;
public StockSpanner() {
stock = new Stack<Stock>();
}
public int next(int price) {
int span = 1;
while(!stock.isEmpty() && stock.peek().price < price){
Stock s = stock.pop();
span += s.span;
}
stock.push(new Stock(price,span));
return span;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/