-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path901.java
More file actions
61 lines (51 loc) · 1.64 KB
/
901.java
File metadata and controls
61 lines (51 loc) · 1.64 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
51
52
53
54
55
56
57
58
59
60
61
__________________________________________________________________________________________________
sample 71 ms submission
class StockSpanner {
private int[] prices;
private int[] ndays;
private int size;
public StockSpanner() {
this.prices = new int[10001];
this.ndays = new int[10001];
size = 0;
prices[size] = 1000000;
ndays[size] = 1;
}
public int next(int price) {
int days = 1;
while (prices[size] <= price) {
//int prev_price = prices[size];
int prev_ndays = ndays[size];
days += prev_ndays;
size--;
}
size++;
prices[size] = price;
ndays[size] = days;
return days;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/
__________________________________________________________________________________________________
sample 64924 kb submission
class StockSpanner {
private List<Integer> list = new ArrayList<>();
private List<Integer> prev = new ArrayList<>();
public int next(int price) {
list.add(price);
int i = list.size() - 2;
while (!prev.isEmpty() && i >= 0 && list.get(i) <= price) i = prev.get(i);
prev.add(i);
return list.size() - 1 - i;
}
}
/**
* Your StockSpanner object will be instantiated and called as such:
* StockSpanner obj = new StockSpanner();
* int param_1 = obj.next(price);
*/
__________________________________________________________________________________________________