KK's blog

每天积累多一些

0%

LeetCode 2034 Stock Price Fluctuation

LeetCode



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]

Explanation
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5); // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current(); // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum(); // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3); // The previous timestamp 1 had the wrong price, so it is updated to 3.
// Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum(); // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2); // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum(); // return 2, the minimum price is 2 at timestamp 4.


Constraints:
1 <= timestamp, price <= 10<sup>9</sup>
At most 10<sup>5</sup> calls will be made in total to update, current, maximum, and minimum. current, maximum, and minimum will be called only after update has been called at least once.

题目大意:

实现一个关于股票的数据结构,可以更新时间点对应的股价,最大最小值,最新价格

解题思路:

求最大最小值容易想到用heap,但heap不支持更新,难点是怎么支持更新股价。
仍然(price, timestamp)加入到heap中,在出堆时验证

解题步骤:

N/A

注意事项:

  1. 验证堆顶: 若股价和时间不匹配(用time_to_price验证),表示这是stale股价,不断去掉,直到验证成功为止,最后加入到堆中

Python代码:

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
class StockPrice(TestCases):

def __init__(self):
self.time_to_price = {}
self.cur_time = 0
self.min_heap = []
self.max_heap = []

def update(self, timestamp: int, price: int) -> None:
self.time_to_price[timestamp] = price
self.cur_time = max(self.cur_time, timestamp)
heapq.heappush(self.min_heap, (price, timestamp))
heapq.heappush(self.max_heap, (-price, timestamp))

def current(self) -> int:
return self.time_to_price[self.cur_time]

def maximum(self) -> int:
price, timestamp = heapq.heappop(self.max_heap)
while -price != self.time_to_price[timestamp]:
price, timestamp = heapq.heappop(self.max_heap)
heapq.heappush(self.max_heap, (price, timestamp))
return -price

def minimum(self) -> int:
price, timestamp = heapq.heappop(self.min_heap)
while price != self.time_to_price[timestamp]:
price, timestamp = heapq.heappop(self.min_heap)
heapq.heappush(self.min_heap, (price, timestamp))
return price

算法分析:

update时间复杂度为O(logn),空间复杂度O(1)

Free mock interview