给你一支股票价格的数据流。数据流中每一条记录包含一个 时间戳 和该时间点股票对应的 价格 。
不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。
请你设计一个算法,实现:
更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。
找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。
找到当前记录里股票的 最高价格 。
找到当前记录里股票的 最低价格 。
请你实现 StockPrice 类:
StockPrice() 初始化对象,当前无股票价格记录。
void update(int timestamp, int price) 在时间点 timestamp 更新股票价格为 price 。
int current() 返回股票 最新价格 。
int maximum() 返回股票 最高价格 。
int minimum() 返回股票 最低价格 。
来源:力扣(LeetCode)
class StockPrice {
/**
* 记录时间戳到价格的映射
*/
private Map<Integer, Integer> map;
/**
* 记录某个价格出现过几次
* 可以方便地返回最大价格和最小价格
*/
private TreeMap<Integer, Integer> treeMap;
/**
* 记录最大的时间戳
*/
private int maxTimestamp;
public StockPrice() {
this.map = new HashMap<>();
this.treeMap = new TreeMap<>();
this.maxTimestamp = 0;
}//这个是为了调用需要的表
public void update(int timestamp, int price) {
// 记录新时间戳到价格的映射
Integer oldPrice = map.put(timestamp, price);
// 如果存在旧价格,则旧价格的次数减一
// 如果旧价格只出现过一次,直接移除
if (oldPrice != null) {
int oldCount = treeMap.get(oldPrice);
if (oldCount == 1) {
treeMap.remove(oldPrice);
} else {
treeMap.put(oldPrice, oldCount - 1);
}
}
// 更新新价格的次数
treeMap.put(price, treeMap.getOrDefault(price, 0) + 1);
// 记录最大时间戳
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
}
public int current() {
return map.get(maxTimestamp);
}
public int maximum() {
return treeMap.lastKey();
}
public int minimum() {
return treeMap.firstKey();
}
}