Leetcode 2034: Stock Price Fluctuation

grid47
grid47
Exploring patterns and algorithms
Apr 17, 2024 6 min read

You are given a stream of stock price records, where each record contains a timestamp and the corresponding price of the stock. The records may not be in order, and some records may contain errors that need to be corrected. Your task is to design an algorithm that updates the stock price at a given timestamp, finds the current price at the latest timestamp, and also finds the maximum and minimum prices the stock has been based on the current records.
Problem
Approach
Steps
Complexity
Input: The input consists of a sequence of operations: 'update' to update the stock price at a timestamp, and queries like 'current', 'maximum', and 'minimum' to retrieve information about the stock price.
Example: [['StockPrice', 'update', 'update', 'current', 'maximum', 'update', 'maximum', 'update', 'minimum'], [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]]
Constraints:
• 1 <= timestamp, price <= 10^9
• At most 10^5 operations in total
• 'current', 'maximum', and 'minimum' will be called only after at least one 'update' operation
Output: The output consists of a list of results corresponding to each operation. For 'update' operations, the result is null. For 'current', 'maximum', and 'minimum' operations, the result is the respective stock price.
Example: [null, null, null, 5, 10, null, 5, null, 2]
Constraints:
• Ensure that each operation is performed in constant or logarithmic time for efficient handling of up to 10^5 operations.
Goal: The goal is to implement a class that can handle stock price updates and queries efficiently, keeping track of the latest, maximum, and minimum stock prices.
Steps:
• 1. Store the stock prices in a map, where the key is the timestamp and the value is the price.
• 2. For 'update' operations, if the timestamp already exists, update the price; otherwise, add a new entry for the timestamp.
• 3. Keep track of the maximum and minimum prices using additional variables or sorted data structures.
• 4. For 'current', 'maximum', and 'minimum' queries, simply return the corresponding price based on the latest records.
Goal: The constraints ensure that the solution is efficient enough to handle large inputs and multiple operations.
Steps:
• 1 <= timestamp, price <= 10^9
• At most 10^5 operations will be made in total.
Assumptions:
• The timestamps are positive integers and can be updated multiple times.
• The 'current', 'maximum', and 'minimum' queries will only occur after at least one 'update' operation.
Input: ['StockPrice', 'update', 'update', 'current', 'maximum', 'update', 'maximum', 'update', 'minimum']
Explanation: In this example, the stock prices are updated at timestamps 1 and 2. The 'current' operation returns the latest price at timestamp 2, which is 5. The 'maximum' operation returns the highest price seen so far, which is 10. After updating the price at timestamp 1, the 'maximum' becomes 5, and after adding a price at timestamp 4, the 'minimum' becomes 2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus