-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2034. Stock Price Fluctuation.py
73 lines (54 loc) · 2.54 KB
/
2034. Stock Price Fluctuation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
""" 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, correcting the price from any previous records at the timestamp.
Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
Finds the maximum price the stock has been based on the current records.
Finds the minimum price the stock has been based on the current records.
Implement the StockPrice class:
StockPrice() Initializes the object with no price records.
void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
int current() Returns the latest price of the stock.
int maximum() Returns the maximum price of the stock.
int minimum() Returns the minimum price of the stock.
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]
"""
class StockPrice:
def __init__(self):
self.mp = collections.defaultdict(int)
self.mx_t = -math.inf
self.mxhp = []
self.mnhp = []
def update(self, timestamp: int, price: int) -> None:
self.mp[timestamp] = price
heapq.heappush(self.mxhp, (-price, timestamp))
heapq.heappush(self.mnhp, (price, timestamp))
self.mx_t = max(timestamp, self.mx_t)
return
def current(self) -> int:
return self.mp[self.mx_t]
def maximum(self) -> int:
p, t = self.mxhp[0]
p = -p
while self.mp[t] != p:
heapq.heappop(self.mxhp)
p, t = self.mxhp[0]
p = -p
return p
def minimum(self) -> int:
p, t = self.mnhp[0]
while self.mp[t] != p:
heapq.heappop(self.mnhp)
p, t = self.mnhp[0]
return p
# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()