LeetCode 2034. 股票价格波动(set + map)

简介: LeetCode 2034. 股票价格波动(set + map)

文章目录

1. 题目

2. 解题

1. 题目

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


不巧的是,由于股票市场内在的波动性,股票价格记录可能不是按时间顺序到来的。

某些情况下,有的记录可能是错的。如果两个有相同时间戳的记录出现在数据流中,前一条记录视为错误记录,后出现的记录 更正 前一条错误的记录。


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


更新 股票在某一时间戳的股票价格,如果有之前同一时间戳的价格,这一操作将 更正 之前的错误价格。

找到当前记录里 最新股票价格 。最新股票价格 定义为时间戳最晚的股票价格。

找到当前记录里股票的 最高价格 。

找到当前记录里股票的 最低价格 。

请你实现 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 <= 10^9
update,current,maximum 和 minimum 总 调用次数不超过 10^5 。
current,maximum 和 minimum 被调用时,update 操作 至少 已经被调用过 一次 。


2. 解题

  • set 有序
struct cmp{
    bool operator()(pair<int,int> a, pair<int,int> b) const
    {
        if(a.second == b.second)
            return a.first < b.first;//这句必须写,不然只按second检查,second可能有重复
            // 删除的时候,按值删除,会删除多个 item 
        return a.second < b.second;
    }
};
class StockPrice {
    int curtime = 0, curprice = 0;
    set<pair<int,int>, cmp> s; // 记录 <time, price> 按价钱排序
    unordered_map<int, int> t_p; // 记录 time 对应的 price
public:
    StockPrice() {
    }
    void update(int timestamp, int price) {
        if(timestamp >= curtime)
        {
            curtime = timestamp;
            curprice = price;
        }
        if(t_p.find(timestamp) != t_p.end())
            s.erase({timestamp, t_p[timestamp]});
        s.insert({timestamp, price});
        t_p[timestamp] = price;
    }
    int current() {
        return curprice;
    }
    int maximum() {
        return (--s.end())->second;
    }
    int minimum() {
        return s.begin()->second;
    }
};

412 ms 163.4 MB C++

相关文章
|
6天前
|
存储 JavaScript 索引
js开发:请解释什么是ES6的Map和Set,以及它们与普通对象和数组的区别。
ES6引入了Map和Set数据结构。Map的键可以是任意类型且有序,与对象的字符串或符号键不同;Set存储唯一值,无重复。两者皆可迭代,支持for...of循环。Map有get、set、has、delete等方法,Set有add、delete、has方法。示例展示了Map和Set的基本操作。
25 3
|
6天前
|
存储 数据格式
Set和Map的应用场景
Set和Map的应用场景
|
6天前
|
算法 索引
leetcode代码记录(买卖股票的最佳时机
leetcode代码记录(买卖股票的最佳时机
13 1
|
5天前
|
JavaScript 前端开发 Java
ES6 逐点突破系列 -- Set Map,工作感悟,完美收官
ES6 逐点突破系列 -- Set Map,工作感悟,完美收官
|
5天前
|
存储 缓存 JavaScript
JavaScript中的Set和Map:理解与使用
JavaScript中的Set和Map:理解与使用
|
6天前
|
存储 JavaScript
ES6+新特性-Symbol与Set/Map数据结构
ES6 引入了三种新的数据结构:Symbol、Set和Map。Symbol是唯一且不可变的值,常用于定义对象的独特属性;Set存储不重复值,适合数组去重;Map则是键值对集合,键可为任意类型,提供了更灵活的存储方式。这些新数据结构提供了更高效的操作手段,分别解决了属性命名冲突、数据去重和复杂键值对存储的问题。示例展示了如何使用Symbol、Set和Map进行基本操作。
|
6天前
|
存储 编译器 C++
C++:map&set 对红黑树的封装
C++:map&set 对红黑树的封装
11 1
|
6天前
|
存储
Map与Set的经典OJ题
Map与Set的经典OJ题
13 3
|
6天前
|
存储 自然语言处理 容器
Map与Set
Map与Set
13 3
|
6天前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
16 4