2034.股票价格波动

2034. 股票价格波动 - 力扣(LeetCode)

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

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

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

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

请你实现 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
  • updatecurrentmaximumminimum 调用次数不超过 105
  • currentmaximumminimum 被调用时,update 操作 至少 已经被调用过 一次

思路:

这个题目就是哈希表加优先队列;首先设置了俩个队列,一个是用来存储股票的最高价格,一个是存储最小价格。在队列中找到相同的日期的时候,就更新价格;

完整代码:

java 复制代码
class StockPrice {
    int max;
    HashMap<Integer, Integer> PriceMap;
    PriorityQueue<int[]> pqMax;
    PriorityQueue<int[]> pqMin;

    public StockPrice() {
        max = 0;
        PriceMap = new HashMap<Integer, Integer>();
        pqMax = new PriorityQueue<int[]>((a, b) -> b[0] - a[0]);
        pqMin = new PriorityQueue<int[]>((a, b) -> a[0] - b[0]);
    }

    public void update(int timestamp, int price) {
        max = Math.max(max, timestamp);
        PriceMap.put(timestamp, price);
        pqMax.offer(new int[]{price, timestamp});
        pqMin.offer(new int[]{price, timestamp});
    }

    public int current() {
        return PriceMap.get(max);
    }

    public int maximum() {
        while (true) {
            int[] priceTime = pqMax.peek();
            int price = priceTime[0], timestamp = priceTime[1];
            if (PriceMap.get(timestamp) == price) {
                return price;
            }
            pqMax.poll();
        }
    }

    public int minimum() {
        while (true) {
            int[] priceTime = pqMin.peek();
            int price = priceTime[0], timestamp = priceTime[1];
            if (PriceMap.get(timestamp) == price) {
                return price;
            }
            pqMin.poll();
        }
    }
}

/**
 * Your StockPrice object will be instantiated and called as such:
 * StockPrice obj = new StockPrice();
 * obj.update(timestamp,price);
 * int param_2 = obj.current();
 * int param_3 = obj.maximum();
 * int param_4 = obj.minimum();
 */
相关推荐
babytiger7 小时前
Gitea 重安装 + Snap 数据迁移完整流程总结
linux·elasticsearch·gitea
匆匆那年9677 小时前
远程 Linux 校园网认证操作手册(本地浏览器法)
linux·运维·服务器
dog2508 小时前
为何新增网络路径反而引入额外时延
服务器·网络·php
newnazi8 小时前
RedHat10 安装MS SQL Server2025
linux·服务器·数据库
zkkkkkkkkkkkkk8 小时前
Linux进行管理工具Supervisor配置与使用
linux·python·supervisor
爱喝水的鱼丶8 小时前
SAP-ABAP:ABAP函数 NUMBER_GET_NEXT 详解:从编号范围对象获取下一个编号
运维·数据库·学习·sap·abap
开发者联盟league8 小时前
linux普通用户使用pip安装模块
linux·python·pip
QuestLab8 小时前
③-进阶篇:vLLM实战——多卡部署、压测与排障
linux·服务器·网络
Languorous.9 小时前
Windows 安装 Linux 虚拟机 / WSL 完整教程(新手零失败)
linux·运维·windows
ElevenS_it1889 小时前
K8s容器环境运维监控盲区:从Node到Pod到Service的可观测性分层实战
运维·容器·kubernetes