我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第1篇文章,点击查看活动详情
每日刷题 2022.09.10
- leetcode原题链接:leetcode.cn/problems/st…
- 难度:中等
- 方法:优先队列
题目
- 给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。
- 不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。
- 请你设计一个算法,实现:
- 更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。
- 找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。
- 找到当前记录里股票的 最高价格 。
- 找到当前记录里股票的 最低价格 。
- 请你实现 StockPrice 类:
- StockPrice() 初始化对象,当前无股票价格记录。
- void update(int timestamp, int price) 在时间点 timestamp 更新股票价格为 price 。
- int current() 返回股票 最新价格 。
- int maximum() 返回股票 最高价格 。
- int minimum() 返回股票 最低价格 。
示例
输入:
["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
- update,current,maximum 和 minimum 总 调用次数不超过 105 。
- current,maximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。
解题思路
- 两个优先队列,用于记录:最大值和最小值
- 使用一个对象
obj,用于记录:最晚的时间和价格 - 使用
map集合,记录所有的正确的时间和价格
优先队列
leetcode提供了优先队列的库,可以直接使用。- 常用的操作:入队
enqueue,出队dequeue,获取队头的元素front,排序compare函数。针对于可能会更新优先队列中的数据的时候,会比较难,如果只是更新其部分数据,可以通过弹出的时候,循环判断来找到合适的。
注意事项
- !!注意:不能只使用一个值全程来维护最大/小值,因为如果当需要
update更新的价格小于最大值,大于最小值的时候,就需要找到次小/大值,但是没有记录。这样就会需要遍历所有的数据,想想每次调用min/max的时候,都需要遍历一遍,就知道时间复杂度是很大的(回家等通知解法)。 - 那么就可以在全局维护两个优先队列,每次
update的时候,就只需要往优先队列中加入当前的值就可以了。在想要获取最大值或者最小值的时候,直接从优先队列中取队头的数就可以了。 - 但是还存在一个问题:就是当我们
update已经存在的数据的时候,优先队列中并没有办法直接找到其对应的值拿出来进行修改。那么就需要在取最大/小值的时候,循环取队头的数,如果是update之前的数,就从队列中删除;否则就直接返回其价格price,就可以获取到最大/小值了。
AC代码
var StockPrice = function() {
this.map = new Map();
this.obj = {
time: -1,
price: -1
};
// 可以整体排序,避免每一次都需要排序
this.maxQueue = new MaxPriorityQueue({
compare: (e1,e2) => e2.price - e1.price
});
this.minQueue = new MinPriorityQueue({
compare: (e1,e2) => e1.price - e2.price
});
};
/**
* @param {number} timestamp
* @param {number} price
* @return {void}
*/
StockPrice.prototype.update = function(timestamp, price) {
this.map.set(timestamp, price);
this.maxQueue.enqueue({
time: timestamp,
price: price
});
this.minQueue.enqueue({
time: timestamp,
price: price
});
if(this.obj.time <= timestamp) {
this.obj.price = price
this.obj.time = timestamp
}
};
/**
* @return {number}
*/
StockPrice.prototype.current = function() {
return this.obj.price;
};
/**
* @return {number}
*/
StockPrice.prototype.maximum = function() {
// 判断当前的是否是正确的
while(true) {
let cur = this.maxQueue.front(), t = this.map.get(cur.time);
if(cur.price === t) {
return cur.price;
}else this.maxQueue.dequeue();
}
};
/**
* @return {number}
*/
StockPrice.prototype.minimum = function() {
while(true) {
let cur = this.minQueue.front(), t = this.map.get(cur.time);
if(cur.price === t) {
return cur.price;
}else this.minQueue.dequeue();
}
};
时间复杂度
- 时间复杂度:令
n为最大调用次数,update复杂度为O(logn);current、maximum 和 minimum - 操作复杂度为
O(1) - 空间复杂度:
O(n)