2034. Stock Price Fluctuation LeetCode Solution
In this guide, you will get 2034. Stock Price Fluctuation LeetCode Solution with the best time and space complexity. The solution to Stock Price Fluctuation problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Stock Price Fluctuation solution in C++
- Stock Price Fluctuation solution in Java
- Stock Price Fluctuation solution in Python
- Additional Resources

Problem Statement of 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.
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.
Complexity Analysis
- Time Complexity: O(\log n)
- Space Complexity: O(|\texttt{update()}|
2034. Stock Price Fluctuation LeetCode Solution in C++
class StockPrice {
public:
void update(int timestamp, int price) {
if (timestampToPrice.contains(timestamp)) {
const int prevPrice = timestampToPrice[timestamp];
if (--pricesCount[prevPrice] == 0)
pricesCount.erase(prevPrice);
}
timestampToPrice[timestamp] = price;
++pricesCount[price];
}
int current() {
return timestampToPrice.rbegin()->second;
}
int maximum() {
return pricesCount.rbegin()->first;
}
int minimum() {
return pricesCount.begin()->first;
}
private:
map<int, int> timestampToPrice;
map<int, int> pricesCount;
};
/* code provided by PROGIEZ */
2034. Stock Price Fluctuation LeetCode Solution in Java
class StockPrice {
public void update(int timestamp, int price) {
if (timestampToPrice.containsKey(timestamp)) {
final int prevPrice = timestampToPrice.get(timestamp);
if (pricesCount.merge(prevPrice, -1, Integer::sum) == 0)
pricesCount.remove(prevPrice);
}
timestampToPrice.put(timestamp, price);
pricesCount.merge(price, 1, Integer::sum);
}
public int current() {
return timestampToPrice.lastEntry().getValue();
}
public int maximum() {
return pricesCount.lastKey();
}
public int minimum() {
return pricesCount.firstKey();
}
private TreeMap<Integer, Integer> timestampToPrice = new TreeMap<>();
private TreeMap<Integer, Integer> pricesCount = new TreeMap<>();
}
// code provided by PROGIEZ
2034. Stock Price Fluctuation LeetCode Solution in Python
from sortedcontainers import SortedDict
class StockPrice:
def __init__(self):
self.timestampToPrice = SortedDict()
self.pricesCount = SortedDict()
def update(self, timestamp: int, price: int) -> None:
if timestamp in self.timestampToPrice:
prevPrice = self.timestampToPrice[timestamp]
self.pricesCount[prevPrice] -= 1
if self.pricesCount[prevPrice] == 0:
del self.pricesCount[prevPrice]
self.timestampToPrice[timestamp] = price
self.pricesCount[price] = self.pricesCount.get(price, 0) + 1
def current(self) -> int:
return self.timestampToPrice.peekitem(-1)[1]
def maximum(self) -> int:
return self.pricesCount.peekitem(-1)[0]
def minimum(self) -> int:
return self.pricesCount.peekitem(0)[0]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.