[LeetCode] Stock Price Fluctuation

2034. Stock Price Fluctuation

You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

Implement the StockPrice class:

  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.
  • new solution update 2022.05.13
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 StockPrice {
unordered_map<int, int> mp;
int ct = -1, cc = 0;
priority_queue<pair<int, int>> ma;
priority_queue<pair<int, int>, vector<pair<int,int>>, greater<pair<int,int>>> mi;

public:
StockPrice() {}

void update(int timestamp, int price) {
mp[timestamp] = price;
ma.push({price, timestamp});
mi.push({price, timestamp});
if(ct <= timestamp) {
ct = timestamp;
cc = price;
}
}

int current() {
return cc;
}

int maximum() {
while(!ma.empty()) {
auto [price, time] = ma.top();
if(mp[time] == price) break;
ma.pop();
}
return ma.top().first;
}

int minimum() {
while(!mi.empty()) {
auto [price, time] = mi.top();
if(mp[time] == price) break;
mi.pop();
}
return mi.top().first;
}
};

/**
* 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();
*/
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
class StockPrice {
map<int, int> stockWithTimeStamp;
priority_queue<int, vector<int>, greater<int>> minimumPrices, wrongMinPrices;
priority_queue<int> maximumPrices, wrongMaxPrices;
public:
StockPrice() {}

void update(int timestamp, int price) {
if(stockWithTimeStamp.count(timestamp)) {
wrongMaxPrices.push(stockWithTimeStamp[timestamp]);
wrongMinPrices.push(stockWithTimeStamp[timestamp]);
}
maximumPrices.push(price);
minimumPrices.push(price);
stockWithTimeStamp[timestamp] = price;
}

int current() {
return stockWithTimeStamp.rbegin()->second;
}

int maximum() {
while(!wrongMaxPrices.empty() and maximumPrices.top() == wrongMaxPrices.top()) {
maximumPrices.pop();
wrongMaxPrices.pop();
}
return maximumPrices.top();
}

int minimum() {
while(!wrongMinPrices.empty() and minimumPrices.top() == wrongMinPrices.top()) {
minimumPrices.pop();
wrongMinPrices.pop();
}
return minimumPrices.top();
}
};

/**
* 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();
*/
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/12/PS/LeetCode/stock-price-fluctuation/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.