Skip to content

Practice Stock Price Fluctuation

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2034
LeetCode Problem

Stock Price Fluctuation

Problem Statement

StockPrice.update(timestamp,price) records or corrects that timestamp's price. current() returns the price at the greatest recorded timestamp, not the last update received. maximum() and minimum() use only current corrected records. Query methods follow at least one update. Practice approach: Keep a timestamp-to-price map and the maximum timestamp. Maintain an ordered multiset of current prices, removing the replaced price on correction. Alternatively use min/max heaps with lazy deletion against the map. Test a late correction that invalidates an extreme without changing the latest timestamp.

Source: Stock Price Fluctuation on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["StockPrice","update","update","current","maximum","update","maximum","minimum","current"]
[[],[5,40],[2,70],[],[],[2,20],[],[],[]]
Output
[null,null,null,40,70,null,40,20,40]
Explanation

The latest timestamp remains 5. Correcting timestamp 2 removes the old high of 70 and introduces the low of 20.

Constraints

1 <= timestamp, price <= 10^9
At most 10^5 total calls.

Related LeetCode Problems

Practice more problems similar to Stock Price Fluctuation .

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.