##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2034.html

# 2034. Stock Price Fluctuation (Medium)

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.

Example 1:

Input
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output
[null, null, null, 5, 10, null, 5, null, 2]

Explanation
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5);  // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current();     // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum();     // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3);  // The previous timestamp 1 had the wrong price, so it is updated to 3.
// Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum();     // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2);  // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum();     // return 2, the minimum price is 2 at timestamp 4.

Constraints:

• 1 <= timestamp, price <= 109
• At most 105 calls will be made in total to update, current, maximum, and minimum.
• current, maximum, and minimum will be called only after update has been called at least once.

Similar Questions:

## Solution 1.

• class StockPrice {
private int lastTs;
private Map<Integer, Integer> mp = new HashMap<>();
private TreeMap<Integer, Integer> counter = new TreeMap<>();

public StockPrice() {
}

public void update(int timestamp, int price) {
if (mp.containsKey(timestamp)) {
int oldPrice = mp.get(timestamp);
counter.put(oldPrice, counter.get(oldPrice) - 1);
if (counter.get(oldPrice) == 0) {
counter.remove(oldPrice);
}
}
mp.put(timestamp, price);
counter.put(price, counter.getOrDefault(price, 0) + 1);
lastTs = Math.max(lastTs, timestamp);
}

public int current() {
return mp.get(lastTs);
}

public int maximum() {
return counter.lastKey();
}

public int minimum() {
return counter.firstKey();
}
}

/**
* 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();
*/

• class StockPrice {
public:
int lastTs;
unordered_map<int, int> mp;
map<int, int> counter;

StockPrice() {
}

void update(int timestamp, int price) {
if (mp.count(timestamp)) {
int oldPrice = mp[timestamp];
--counter[oldPrice];
if (counter[oldPrice] == 0) counter.erase(oldPrice);
}
mp[timestamp] = price;
++counter[price];
lastTs = max(lastTs, timestamp);
}

int current() {
return mp[lastTs];
}

int maximum() {
return counter.rbegin()->first;
}

int minimum() {
return counter.begin()->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();
*/

• from sortedcontainers import SortedDict

class StockPrice:
def __init__(self):
self.last_ts = 0
self.mp = {}
self.counter = SortedDict()

def update(self, timestamp: int, price: int) -> None:
if timestamp in self.mp:
old_price = self.mp[timestamp]
self.counter[old_price] -= 1
if self.counter[old_price] == 0:
del self.counter[old_price]
if price not in self.counter:
self.counter[price] = 0
self.counter[price] += 1
self.mp[timestamp] = price
self.last_ts = max(self.last_ts, timestamp)

def current(self) -> int:
return self.mp[self.last_ts]

def maximum(self) -> int:
return self.counter.keys()[-1]

def minimum(self) -> int:
return self.counter.keys()[0]

# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()

• type StockPrice struct {
lastTs  int
mp      map[int]int
counter *redblacktree.Tree
}

func Constructor() StockPrice {
return StockPrice{
mp:      make(map[int]int),
counter: redblacktree.NewWithIntComparator(),
}
}

func (this *StockPrice) Update(timestamp int, price int) {
if timestamp > this.lastTs {
this.lastTs = timestamp
}
if old, ok := this.mp[timestamp]; ok {
cnt := getInt(this.counter, old)
if cnt == 1 {
this.counter.Remove(old)
} else {
this.counter.Put(old, cnt-1)
}
}
this.mp[timestamp] = price
this.counter.Put(price, getInt(this.counter, price)+1)
}

func (this *StockPrice) Current() int {
return this.mp[this.lastTs]
}

func (this *StockPrice) Maximum() int {
return this.counter.Right().Key.(int)
}

func (this *StockPrice) Minimum() int {
return this.counter.Left().Key.(int)
}

func getInt(rbt *redblacktree.Tree, key int) int {
val, found := rbt.Get(key)
if !found {
return 0
}
return val.(int)
}

/**
* Your StockPrice object will be instantiated and called as such:
* obj := Constructor();
* obj.Update(timestamp,price);
* param_2 := obj.Current();
* param_3 := obj.Maximum();
* param_4 := obj.Minimum();
*/