Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Last active October 10, 2021 04:40
Show Gist options
  • Save sreeprasad/dad58c9cd0b03dae35e0a53307f857c9 to your computer and use it in GitHub Desktop.
Save sreeprasad/dad58c9cd0b03dae35e0a53307f857c9 to your computer and use it in GitHub Desktop.
wrong_ans_prob_3.java
class StockPrice {
TreeMap<Integer,Integer> timeToPrice;
TreeMap<Integer,Integer> priceToTime;
int current;
public StockPrice() {
timeToPrice = new TreeMap<>();
priceToTime = new TreeMap<>();
current =0;
}
public void update(int timestamp, int price) {
if (timeToPrice.containsKey(timestamp)) {
priceToTime.remove(timeToPrice.get(timestamp));
} else {
current = price;
}
timeToPrice.put(timestamp, price);
priceToTime.put(price, timestamp);
}
public int current() {
return current;
}
public int maximum() {
return priceToTime.lastKey();
}
public int minimum() {
return priceToTime.firstKey();
}
}
/**
[1, 10]
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[null, null, null, 5, 10, null, 5, null, 2]
[null, null, null, 5, 1, null, 1, null, 4]
* 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();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment