2034.股票价格波动

【LetMeFly】2034.股票价格波动:哈希表 + 有序集合

力扣题目链接:https://leetcode.cn/problems/stock-price-fluctuation/

给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。

不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。

请你设计一个算法,实现:

  • 更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。
  • 找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。
  • 找到当前记录里股票的 最高价格 。
  • 找到当前记录里股票的 最低价格 。

请你实现 StockPrice 类:

  • StockPrice() 初始化对象,当前无股票价格记录。
  • void update(int timestamp, int price) 在时间点 timestamp 更新股票价格为 price 。
  • int current() 返回股票 最新价格 。
  • int maximum() 返回股票 最高价格 。
  • int minimum() 返回股票 最低价格 。

 

示例 1:

输入:
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
输出:
[null, null, null, 5, 10, null, 5, null, 2]

解释:
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // 时间戳为 [1] ,对应的股票价格为 [10] 。
stockPrice.update(2, 5);  // 时间戳为 [1,2] ,对应的股票价格为 [10,5] 。
stockPrice.current();     // 返回 5 ,最新时间戳为 2 ,对应价格为 5 。
stockPrice.maximum();     // 返回 10 ,最高价格的时间戳为 1 ,价格为 10 。
stockPrice.update(1, 3);  // 之前时间戳为 1 的价格错误,价格更新为 3 。
                          // 时间戳为 [1,2] ,对应股票价格为 [3,5] 。
stockPrice.maximum();     // 返回 5 ,更正后最高价格为 5 。
stockPrice.update(4, 2);  // 时间戳为 [1,2,4] ,对应价格为 [3,5,2] 。
stockPrice.minimum();     // 返回 2 ,最低价格时间戳为 4 ,价格为 2 。

 

提示:

  • 1 <= timestamp, price <= 109
  • updatecurrentmaximum 和 minimum  调用次数不超过 105 。
  • currentmaximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。

方法一:哈希表 + 有序集合

只需要维护三个变量:

  • 哈希表ma用来将时间戳映射为价格
  • 有序集合se(例如C++的multiset)用来存储所有的股票价格
  • 整数Mtime用来存最新的时间戳

那么:

  • 对于update操作,如果哈希表ma中已经存在了这个时间戳,就删除有序集合se中这个时间戳对应的价格。然后更新maseMtime
  • 对于current操作,直接返回哈希表ma中最新时间戳Mtime对应的价格
  • 对于maximum操作,直接返回有序集合se中的最后一个元素
  • 对于minimum操作,直接返回有序集合se中的第一个元素

完毕。

  • 时间复杂度:单次操作涉及有序集合增删的复杂的为$O(\log n)$,否则复杂度为$O(1)$
  • 空间复杂度:$O(n)$,其中$n$是不用的时间戳数量

AC代码

C++

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
class StockPrice {
private:
unordered_map<int, int> ma;
multiset<int> se;
int Mtime;
public:
StockPrice() {
Mtime = 0;
}

void update(int timestamp, int price) {
if (ma.count(timestamp)) {
se.erase(se.find(ma[timestamp]));
}
ma[timestamp] = price;
se.insert(price);
Mtime = max(Mtime, timestamp);
}

int current() {
return ma[Mtime];
}

int maximum() {
return *se.rbegin();
}

int minimum() {
return *se.begin();
}
};

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
# from sortedcontainers import SortedList


class StockPrice:

def __init__(self):
self.ma = {}
self.se = SortedList()
self.Mtime = 0


def update(self, timestamp: int, price: int) -> None:
if timestamp in self.ma:
self.se.discard(self.ma[timestamp])
self.ma[timestamp] = price
self.se.add(price)
self.Mtime = max(self.Mtime, timestamp)


def current(self) -> int:
return self.ma[self.Mtime]


def maximum(self) -> int:
return self.se[-1]


def minimum(self) -> int:
return self.se[0]

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/133677649


2034.股票价格波动
https://blog.letmefly.xyz/2023/10/08/LeetCode 2034.股票价格波动/
作者
Tisfy
发布于
2023年10月8日
许可协议