2034. Stock Price Fluctuation Medium

@problem@discussion
#Hash Table#Design#Heap (Priority Queue)#Data Stream#Ordered Set



1/**
2 * [2034] Stock Price Fluctuation 
3 *
4 * 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.
5 * 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.
6 * Design an algorithm that:
7 * 
8 * 	Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
9 * 	Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
10 * 	Finds the maximum price the stock has been based on the current records.
11 * 	Finds the minimum price the stock has been based on the current records.
12 * 
13 * Implement the StockPrice class:
14 * 
15 * 	StockPrice() Initializes the object with no price records.
16 * 	void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
17 * 	int current() Returns the latest price of the stock.
18 * 	int maximum() Returns the maximum price of the stock.
19 * 	int minimum() Returns the minimum price of the stock.
20 * 
21 *  
22 * Example 1:
23 * 
24 * Input
25 * ["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
26 * [[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
27 * Output
28 * [null, null, null, 5, 10, null, 5, null, 2]
29 * Explanation
30 * StockPrice stockPrice = new StockPrice();
31 * stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
32 * stockPrice.update(2, 5);  // Timestamps are [1,2] with corresponding prices [10,5].
33 * stockPrice.current();     // return 5, the latest timestamp is 2 with the price being 5.
34 * stockPrice.maximum();     // return 10, the maximum price is 10 at timestamp 1.
35 * stockPrice.update(1, 3);  // The previous timestamp 1 had the wrong price, so it is updated to 3.
36 *                           // Timestamps are [1,2] with corresponding prices [3,5].
37 * stockPrice.maximum();     // return 5, the maximum price is 5 after the correction.
38 * stockPrice.update(4, 2);  // Timestamps are [1,2,4] with corresponding prices [3,5,2].
39 * stockPrice.minimum();     // return 2, the minimum price is 2 at timestamp 4.
40 * 
41 *  
42 * Constraints:
43 * 
44 * 	1 <= timestamp, price <= 10^9
45 * 	At most 10^5 calls will be made in total to update, current, maximum, and minimum.
46 * 	current, maximum, and minimum will be called only after update has been called at least once.
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/stock-price-fluctuation/
52// discuss: https://leetcode.com/problems/stock-price-fluctuation/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56struct StockPrice {
57        false
58    }
59
60
61/** 
62 * `&self` means the method takes an immutable reference.
63 * If you need a mutable reference, change it to `&mut self` instead.
64 */
65impl StockPrice {
66
67    fn new() -> Self {
68        
69    }
70    
71    fn update(&self, timestamp: i32, price: i32) {
72        
73    }
74    
75    fn current(&self) -> i32 {
76        
77    }
78    
79    fn maximum(&self) -> i32 {
80        
81    }
82    
83    fn minimum(&self) -> i32 {
84        
85    }
86}
87
88/**
89 * Your StockPrice object will be instantiated and called as such:
90 * let obj = StockPrice::new();
91 * obj.update(timestamp, price);
92 * let ret_2: i32 = obj.current();
93 * let ret_3: i32 = obj.maximum();
94 * let ret_4: i32 = obj.minimum();
95 */
96
97// submission codes end
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_2034() {
105    }
106}
107


Back
© 2025 bowen.ge All Rights Reserved.