Leetcode 2161 Solution
This article provides solution to leetcode question 2161 (stock-price-fluctuation)
Access this page by simply typing in "lcs 2161" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/stock-price-fluctuation
Solution
class StockPrice {
map<int, int> price_map;
set<pair<int, int>> price_set;
public:
StockPrice() {
}
void update(int timestamp, int price) {
auto it = price_map.find(timestamp);
if (it != price_map.end())
{
int old_price = it->second;
price_set.erase(make_pair(old_price, timestamp));
}
price_map[timestamp] = price;
price_set.insert(make_pair(price, timestamp));
}
int current() {
auto it = --price_map.end();
return it->second;
}
int maximum() {
auto it = --price_set.end();
return it->first;
}
int minimum() {
auto it = price_set.begin();
return it->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();
*/