1
class StockPrice {
2
map<int, int> stock;
3
map<int, int> prices;
4
pair<int, int> currentStock = {-1, -1};
5

6
public:
7
StockPrice() {}
8

9
void update(int timestamp, int price) {
10
// Update current stock price if we get a new greater timestamp
11
if (timestamp >= currentStock.first) {
12
currentStock = {timestamp, price};
13
}
14

15
if (stock.find(timestamp) != stock.end()) {
16
// Case 2
17
int old_price = stock[timestamp]; // Get old price for timestamp
18

19
prices[old_price]--; // Reduce count
20
if (prices[old_price] == 0) prices.erase(old_price); // Remove if no timestamp has old price
21
}
22

23
// Case 1
24
prices[price]++;
25
stocks[timestamp] = price;
26
}
27

28
int current() {
29
return currentStock.second;
30
}
31

32
int maximum() {
33
return prices.rbegin()->first; // GET last element
34
}
35

36
int minimum() {
37
return prices.begin()->first; // Get first element
38
}
39
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0