-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDay19-OnlineStockSpan.java
More file actions
50 lines (40 loc) · 1.01 KB
/
Day19-OnlineStockSpan.java
File metadata and controls
50 lines (40 loc) · 1.01 KB
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
44
45
46
47
48
49
50
class StockSpanner {
Stack<SharePrice> priceStack;
int day=0;
public StockSpanner() {
priceStack=new Stack<>();
}
public int next(int price) {
day++;
SharePrice sp=new SharePrice(day,price);
while(!priceStack.isEmpty() && priceStack.peek().getPrice()<=price){
priceStack.pop();
}
if(priceStack.isEmpty()){
priceStack.push(sp);
return day;
}
int res=priceStack.peek().getDay();
priceStack.push(sp);
return day-res;
}
}
class SharePrice{
int day;
int price;
SharePrice(int day,int price){
this.day=day;
this.price=price;
}
public int getDay(){
return day;
}
public int getPrice(){
return price;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/