1
class StockRecord {
2
int timestamp;
3
int price;
4

5
public StockRecord() {}
6

7
public StockRecord(int t, int p) {
8
timestamp = t;
9
price = p;
10
}
11
}
12

13
class StockPrice {
14

15
PriorityQueue<StockRecord> max = new PriorityQueue<>((sr1, sr2) -> (sr2.price - sr1.price));
16
PriorityQueue<StockRecord> min = new PriorityQueue<>((sr1, sr2) -> (sr1.price - sr2.price));
17
StockRecord current_record;
18
Map<Integer, Integer> map = new HashMap<>();
19

20
public StockPrice() {
21
current_record = new StockRecord();
22
}
23

24
public void update(int timestamp, int price) {
25
if (timestamp >= current_record.timestamp) {
26
current_record.timestamp = timestamp;
27
current_record.price = price;
28
}
29

30
StockRecord sr = new StockRecord(timestamp, price);
31
max.add(sr);
32
min.add(sr);
33
map.put(timestamp, price);
34
}
35

36
public int current() {
37
return current_record.price;
38
}
39

40
public int maximum() {
41
StockRecord sp = max.peek();
42
while (true) {
43
sp = max.peek();
44
if (sp.price != map.get(sp.timestamp)) max.poll();
45
else break;
46
}
47
return sp.price;
48
}
49

50
public int minimum() {
51
StockRecord sp = min.peek();
52
while (true) {
53
sp = min.peek();
54
if (sp.price != map.get(sp.timestamp)) min.poll();
55
else break;
56
}
57
return sp.price;
58
}
59
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0