-
Notifications
You must be signed in to change notification settings - Fork 3
/
2034.cpp
44 lines (38 loc) · 1.06 KB
/
2034.cpp
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
class StockPrice {
public:
unordered_map<int, int> timestamp2price; // time->price
pair<int, int> latest; // time->price
multiset<int> apex;
StockPrice() {
latest = make_pair(-1, -1);
}
void update(int timestamp, int price) {
if (timestamp >= latest.first) {
latest.first = timestamp;
latest.second = price;
}
if (timestamp2price.find(timestamp) != timestamp2price.end()) {
int oldPrice = timestamp2price[timestamp];
apex.erase(apex.find(oldPrice));
}
timestamp2price[timestamp] = price;
apex.insert(price);
}
int current() {
return latest.second;
}
int maximum() {
return *apex.rbegin();
}
int minimum() {
return *apex.begin();
}
};
/**
* Your StockPrice object will be instantiated and called as such:
* StockPrice* obj = new StockPrice();
* obj->update(timestamp,price);
* int param_2 = obj->current();
* int param_3 = obj->maximum();
* int param_4 = obj->minimum();
*/