Skip to content

Commit c41e3b6

Browse files
committed
添加了问题“2034. 股票价格波动”的代码和题解
1 parent 103731f commit c41e3b6

File tree

4 files changed

+210
-3
lines changed

4 files changed

+210
-3
lines changed

Codes/2034-stock-price-fluctuation-20231008.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class StockPrice {
1414

1515
void update(int timestamp, int price) {
1616
if (ma.count(timestamp)) {
17-
se.erase(ma[timestamp]);
17+
se.erase(se.find(ma[timestamp]));
1818
}
1919
ma[timestamp] = price;
2020
se.insert(price);
@@ -26,11 +26,11 @@ class StockPrice {
2626
}
2727

2828
int maximum() {
29-
return ma[*se.end()];
29+
return *se.rbegin();
3030
}
3131

3232
int minimum() {
33-
return ma[*se.begin()];
33+
return *se.begin();
3434
}
3535
};
3636

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2023-10-08 12:28:55
4+
LastEditors: LetMeFly
5+
LastEditTime: 2023-10-08 12:34:54
6+
'''
7+
from sortedcontainers import SortedList
8+
9+
10+
class StockPrice:
11+
12+
def __init__(self):
13+
self.ma = {}
14+
self.se = SortedList()
15+
self.Mtime = 0
16+
17+
18+
def update(self, timestamp: int, price: int) -> None:
19+
if timestamp in self.ma:
20+
self.se.discard(self.ma[timestamp])
21+
self.ma[timestamp] = price
22+
self.se.add(price)
23+
self.Mtime = max(self.Mtime, timestamp)
24+
25+
26+
def current(self) -> int:
27+
return self.ma[self.Mtime]
28+
29+
30+
def maximum(self) -> int:
31+
return self.se[-1]
32+
33+
34+
def minimum(self) -> int:
35+
return self.se[0]
36+
37+
38+
# Your StockPrice object will be instantiated and called as such:
39+
# obj = StockPrice()
40+
# obj.update(timestamp,price)
41+
# param_2 = obj.current()
42+
# param_3 = obj.maximum()
43+
# param_4 = obj.minimum()

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ int main() {
450450
|1993.树上的操作|中等|<a href="https://leetcode.cn/problems/operations-on-tree/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/09/23/LeetCode%201993.%E6%A0%91%E4%B8%8A%E7%9A%84%E6%93%8D%E4%BD%9C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133198960" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/operations-on-tree/solutions/2455220/letmefly-1993shu-shang-de-cao-zuo-da-mo-1n5vz/" target="_blank">LeetCode题解</a>|
451451
|2011.执行操作后的变量值|简单|<a href="https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/12/23/LeetCode%202011.%E6%89%A7%E8%A1%8C%E6%93%8D%E4%BD%9C%E5%90%8E%E7%9A%84%E5%8F%98%E9%87%8F%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128423159" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/solutions/2030548/letmefly-2011zhi-xing-cao-zuo-hou-de-bia-tpho/" target="_blank">LeetCode题解</a>|
452452
|2027.转换字符串的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-moves-to-convert-string/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/12/27/LeetCode%202027.%E8%BD%AC%E6%8D%A2%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128453481" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-moves-to-convert-string/solutions/2033896/letmefly-2027zhuan-huan-zi-fu-chuan-de-z-6qcl/" target="_blank">LeetCode题解</a>|
453+
|2034.股票价格波动|中等|<a href="https://leetcode.cn/problems/stock-price-fluctuation/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/10/08/LeetCode%202034.%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/133677649" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/stock-price-fluctuation/solutions/2472177/letmefly-2034gu-piao-jie-ge-bo-dong-ha-x-w3w4/" target="_blank">LeetCode题解</a>|
453454
|2037.使每位学生都有座位的最少移动次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-moves-to-seat-everyone/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/12/31/LeetCode%202037.%E4%BD%BF%E6%AF%8F%E4%BD%8D%E5%AD%A6%E7%94%9F%E9%83%BD%E6%9C%89%E5%BA%A7%E4%BD%8D%E7%9A%84%E6%9C%80%E5%B0%91%E7%A7%BB%E5%8A%A8%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128509361" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-moves-to-seat-everyone/solutions/2039583/letmefly-2037shi-mei-wei-xue-sheng-du-yo-2gpy/" target="_blank">LeetCode题解</a>|
454455
|2042.检查句子中的数字是否递增|简单|<a href="https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/01/03/LeetCode%202042.%E6%A3%80%E6%9F%A5%E5%8F%A5%E5%AD%90%E4%B8%AD%E7%9A%84%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E9%80%92%E5%A2%9E/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128538008" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence/solutions/2043292/letmefly-2042jian-cha-ju-zi-zhong-de-shu-5leg/" target="_blank">LeetCode题解</a>|
455456
|2050.并行课程III|困难|<a href="https://leetcode.cn/problems/parallel-courses-iii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/07/28/LeetCode%202050.%E5%B9%B6%E8%A1%8C%E8%AF%BE%E7%A8%8BIII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131973511" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/parallel-courses-iii/solutions/2362142/letmefly-2050bing-xing-ke-cheng-iiidfs-b-9tuc/" target="_blank">LeetCode题解</a>|
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
---
2+
title: 2034.股票价格波动
3+
date: 2023-10-08 12:36:36
4+
tags: [题解, LeetCode, 中等, 设计, 哈希表, 数据流, 有序集合, 堆(优先队列), set, map]
5+
---
6+
7+
# 【LetMeFly】2034.股票价格波动:哈希表 + 有序集合
8+
9+
力扣题目链接:[https://leetcode.cn/problems/stock-price-fluctuation/](https://leetcode.cn/problems/stock-price-fluctuation/)
10+
11+
<p>给你一支股票价格的数据流。数据流中每一条记录包含一个 <strong>时间戳</strong>&nbsp;和该时间点股票对应的 <strong>价格</strong>&nbsp;。</p>
12+
13+
<p>不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 <b>更正</b>&nbsp;前一条错误的记录。</p>
14+
15+
<p>请你设计一个算法,实现:</p>
16+
17+
<ul>
18+
<li><strong>更新 </strong>股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将&nbsp;<strong>更正</strong>&nbsp;之前的错误价格。</li>
19+
<li>找到当前记录里 <b>最新股票价格</b>&nbsp;。<strong>最新股票价格</strong>&nbsp;定义为时间戳最晚的股票价格。</li>
20+
<li>找到当前记录里股票的 <strong>最高价格</strong>&nbsp;。</li>
21+
<li>找到当前记录里股票的 <strong>最低价格</strong>&nbsp;。</li>
22+
</ul>
23+
24+
<p>请你实现&nbsp;<code>StockPrice</code>&nbsp;类:</p>
25+
26+
<ul>
27+
<li><code>StockPrice()</code>&nbsp;初始化对象,当前无股票价格记录。</li>
28+
<li><code>void update(int timestamp, int price)</code>&nbsp;在时间点 <code>timestamp</code>&nbsp;更新股票价格为 <code>price</code>&nbsp;。</li>
29+
<li><code>int current()</code>&nbsp;返回股票 <strong>最新价格</strong>&nbsp;。</li>
30+
<li><code>int maximum()</code>&nbsp;返回股票 <strong>最高价格</strong>&nbsp;。</li>
31+
<li><code>int minimum()</code>&nbsp;返回股票 <strong>最低价格</strong>&nbsp;。</li>
32+
</ul>
33+
34+
<p>&nbsp;</p>
35+
36+
<p><strong>示例 1:</strong></p>
37+
38+
<pre><strong>输入:</strong>
39+
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
40+
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
41+
<strong>输出:</strong>
42+
[null, null, null, 5, 10, null, 5, null, 2]
43+
44+
<strong>解释:</strong>
45+
StockPrice stockPrice = new StockPrice();
46+
stockPrice.update(1, 10); // 时间戳为 [1] ,对应的股票价格为 [10] 。
47+
stockPrice.update(2, 5); // 时间戳为 [1,2] ,对应的股票价格为 [10,5] 。
48+
stockPrice.current(); // 返回 5 ,最新时间戳为 2 ,对应价格为 5 。
49+
stockPrice.maximum(); // 返回 10 ,最高价格的时间戳为 1 ,价格为 10 。
50+
stockPrice.update(1, 3); // 之前时间戳为 1 的价格错误,价格更新为 3 。
51+
// 时间戳为 [1,2] ,对应股票价格为 [3,5] 。
52+
stockPrice.maximum(); // 返回 5 ,更正后最高价格为 5 。
53+
stockPrice.update(4, 2); // 时间戳为 [1,2,4] ,对应价格为 [3,5,2] 。
54+
stockPrice.minimum(); // 返回 2 ,最低价格时间戳为 4 ,价格为 2 。
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p><strong>提示:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= timestamp, price &lt;= 10<sup>9</sup></code></li>
63+
<li><code>update</code>,<code>current</code>,<code>maximum</code>&nbsp;和&nbsp;<code>minimum</code>&nbsp;<strong>总</strong> 调用次数不超过&nbsp;<code>10<sup>5</sup></code>&nbsp;。</li>
64+
<li><code>current</code>,<code>maximum</code>&nbsp;和&nbsp;<code>minimum</code>&nbsp;被调用时,<code>update</code>&nbsp;操作 <strong>至少</strong>&nbsp;已经被调用过 <strong>一次</strong>&nbsp;。</li>
65+
</ul>
66+
67+
68+
69+
## 方法一:哈希表 + 有序集合
70+
71+
只需要维护三个变量:
72+
73+
+ 哈希表```ma```用来将```时间戳```映射为```价格```
74+
+ 有序集合```se```(例如C++的multiset)用来存储所有的股票价格
75+
+ 整数```Mtime```用来存最新的时间戳
76+
77+
那么:
78+
79+
+ 对于```update```操作,如果哈希表```ma```中已经存在了这个时间戳,就删除有序集合```se```中这个时间戳对应的价格。然后更新```ma``````se``````Mtime```
80+
+ 对于```current```操作,直接返回哈希表```ma```中最新时间戳```Mtime```对应的价格
81+
+ 对于```maximum```操作,直接返回有序集合```se```中的最后一个元素
82+
+ 对于```minimum```操作,直接返回有序集合```se```中的第一个元素
83+
84+
完毕。
85+
86+
+ 时间复杂度:单次操作涉及有序集合增删的复杂的为$O(\log n)$,否则复杂度为$O(1)$
87+
+ 空间复杂度:$O(n)$,其中$n$是不用的时间戳数量
88+
89+
### AC代码
90+
91+
#### C++
92+
93+
```cpp
94+
class StockPrice {
95+
private:
96+
unordered_map<int, int> ma;
97+
multiset<int> se;
98+
int Mtime;
99+
public:
100+
StockPrice() {
101+
Mtime = 0;
102+
}
103+
104+
void update(int timestamp, int price) {
105+
if (ma.count(timestamp)) {
106+
se.erase(se.find(ma[timestamp]));
107+
}
108+
ma[timestamp] = price;
109+
se.insert(price);
110+
Mtime = max(Mtime, timestamp);
111+
}
112+
113+
int current() {
114+
return ma[Mtime];
115+
}
116+
117+
int maximum() {
118+
return *se.rbegin();
119+
}
120+
121+
int minimum() {
122+
return *se.begin();
123+
}
124+
};
125+
```
126+
127+
#### Python
128+
129+
```python
130+
# from sortedcontainers import SortedList
131+
132+
133+
class StockPrice:
134+
135+
def __init__(self):
136+
self.ma = {}
137+
self.se = SortedList()
138+
self.Mtime = 0
139+
140+
141+
def update(self, timestamp: int, price: int) -> None:
142+
if timestamp in self.ma:
143+
self.se.discard(self.ma[timestamp])
144+
self.ma[timestamp] = price
145+
self.se.add(price)
146+
self.Mtime = max(self.Mtime, timestamp)
147+
148+
149+
def current(self) -> int:
150+
return self.ma[self.Mtime]
151+
152+
153+
def maximum(self) -> int:
154+
return self.se[-1]
155+
156+
157+
def minimum(self) -> int:
158+
return self.se[0]
159+
160+
```
161+
162+
> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2023/10/08/LeetCode%202034.%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8/)哦~
163+
> Tisfy:[https://letmefly.blog.csdn.net/article/details/133677649](https://letmefly.blog.csdn.net/article/details/133677649)

0 commit comments

Comments
 (0)