1332. Delete palindrome subsequence
Give you a data stream of stock prices. Each record in the data stream contains a time stamp and the price corresponding to the stock at that time point.
Unfortunately, due to the inherent volatility of the stock market, stock price records may not come in chronological order. In some cases, some records may be wrong. If two records with the same time stamp appear in the data stream, the previous record is regarded as an error record, and the later record corrects the previous error record.
Please design an algorithm to realize:
Update the stock price of the stock at a certain time stamp. If there is a price with the same time stamp before, this operation will correct the previous wrong price.
Find the latest stock price in the current record. The latest stock price is defined as the stock price with the latest timestamp.
Find the highest price of the stock in the current record.
Find the lowest price of the stock in the current record.
Please implement the StockPrice class:
StockPrice() initializes the object. Currently, there is no stock price record.
void update(int timestamp, int price) updates the stock price to price at the time point timestamp.
int current() returns the latest stock price.
int maximum() returns the highest stock price.
int minimum() returns the lowest stock price.
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); // The timestamp is [1], and the corresponding stock price is [10].
stockPrice.update(2, 5); // The timestamp is [1,2], and the corresponding stock price is [10,5].
stockPrice.current(); // Returns 5. The latest timestamp is 2 and the corresponding price is 5.
stockPrice.maximum(); // Returns 10. The timestamp of the highest price is 1 and the price is 10.
stockPrice.update(1, 3); // The previous price with timestamp of 1 is wrong, and the price is updated to 3.
//The timestamp is [1,2], and the corresponding stock price is [3,5].
stockPrice.maximum(); // Return to 5, and the corrected maximum price is 5.
stockPrice.update(4, 2); // The timestamp is [1,2,4], and the corresponding price is [3,5,2].
stockPrice.minimum(); // Returns 2, the lowest price timestamp is 4, and the price is 2.
Tips:
1 <= timestamp, price <= 109
The total number of calls to update, current, maximum and minimum shall not exceed 105.
When current, maximum and minimum are called, the update operation has been called at least once.
Source: LeetCode
Link: https://leetcode-cn.com/problems/stock-price-fluctuation
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.
analysis
Today's daily question
The direct traversal of the value with the hash table will timeout. Here, the priority queue is used (it is OK to use the red black tree, but it has not been learned). For problem solution analysis, refer to the official problem solution method 2.
https://leetcode-cn.com/problems/stock-price-fluctuation/solution/gu-piao-jie-ge-bo-dong-by-leetcode-solut-rwrb/
Problem solving (Java)
class StockPrice { //Create a hash table to store stock timestamps and corresponding prices Map<Integer,Integer> map = new HashMap<>(); //Create variable store maximum timestamp int currentMax = Integer.MIN_VALUE; //Create two priority queues, one in descending order and the other in descending order PriorityQueue<int[]> pqMax = new PriorityQueue<int[]>(((a, b) -> b[0] - a[0])); PriorityQueue<int[]> pqMin = new PriorityQueue<int[]>(((a, b) -> a[0] - b[0])); public StockPrice() { } public void update(int timestamp, int price) { //Store data map.put(timestamp,price); currentMax = Math.max(currentMax, timestamp); //Then create an array to store data. Pay attention to saving price first, because the queue is stored according to the first index size of the array int[] record = {price, timestamp}; pqMax.offer(record); pqMin.offer(record); } public int current() { //Directly return the stock price corresponding to the maximum timestamp return map.get(currentMax); } public int maximum() { //Find the maximum price from the max queue. If it does not correspond to the hash table, it indicates that it has not been updated, delete the current queue header and continue to look forward while (true) { int[] max = pqMax.peek(); int price = max[0], timestamp = max[1]; if (map.get(timestamp) == price) { return price; } pqMax.poll(); } } public int minimum() { //Find the minimum price from the min queue. If it does not correspond to the hash table, it indicates that it is not updated, delete the current queue header and continue to look forward while (true) { int[] min = pqMin.peek(); int price = min[0], timestamp = min[1]; if (map.get(timestamp) == price) { return price; } pqMin.poll(); } } } /** * 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(); */