Stock Price Fluctuation - Complete Solution Guide
Stock Price Fluctuation is LeetCode problem 2034, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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, cor
Detailed Explanation
The problem requires you to design a data structure that simulates a stock price tracking system. The system should handle updates to stock prices at specific timestamps, even if the updates are out of order or correct previous entries. You need to efficiently retrieve the latest price, the maximum price, and the minimum price observed so far. The key challenge is handling potentially unordered and incorrect data while providing fast access to these aggregate values.
Solution Approach
The solution utilizes a hash map (`time_to_price`) to store the price at each timestamp. Two priority queues (max-heap and min-heap) are used to track the maximum and minimum prices, respectively. The 'update' function updates the hash map and adds new price/timestamp pairs to both heaps. The 'current' function simply retrieves the price from the hash map using the latest timestamp. The 'maximum' and 'minimum' functions lazily remove stale entries from the heaps until a valid maximum or minimum price is found, that is price corresponding to that timestamp should exist in time_to_price map with the same price value.
Step-by-Step Algorithm
- Step 1: Initialize the `StockPrice` class with a hash map `time_to_price` to store timestamp-price pairs, a max-heap `max_heap`, a min-heap `min_heap`, and a variable `latest_timestamp` to keep track of the most recent timestamp.
- Step 2: Implement the `update` function: Insert or update the `time_to_price` map with the given timestamp and price. Update `latest_timestamp` if the current timestamp is greater. Push the price-timestamp pair into both `max_heap` and `min_heap`.
- Step 3: Implement the `current` function: Return the price associated with the `latest_timestamp` from the `time_to_price` map.
- Step 4: Implement the `maximum` function: While the `max_heap` is not empty, peek at the top element (maximum price). Check if the price from the heap matches the price stored in `time_to_price` for the given timestamp. If they match, return the price. If they don't match (meaning the price has been updated), pop the element from the heap and continue the loop.
- Step 5: Implement the `minimum` function: While the `min_heap` is not empty, peek at the top element (minimum price). Check if the price from the heap matches the price stored in `time_to_price` for the given timestamp. If they match, return the price. If they don't match (meaning the price has been updated), pop the element from the heap and continue the loop.
Key Insights
- Insight 1: Using a hash map allows for O(1) average time complexity for updating the price at a given timestamp, handling corrections efficiently.
- Insight 2: Priority queues (heaps) are suitable for maintaining maximum and minimum prices. However, due to the possibility of price corrections, lazy deletion is needed, where stale entries in the heaps are removed only when they are accessed.
- Insight 3: Maintaining a separate 'latest_timestamp' variable simplifies the 'current' price retrieval operation.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, Design, Heap (Priority Queue), Data Stream, Ordered Set.
Companies
Asked at: Atlassian, Mixpanel, MongoDB, Ripple.