#2034
Medium
auto_awesome

LeetCode 题解工作台

股票价格波动

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

category

5

题型

code_blocks

4

代码语言

hub

3

相关题

当前训练重点

中等 ·

bolt

答案摘要

我们定义以下几个数据结构或变量,其中: - `d`:表示一个哈希表,用于存储时间戳和对应的价格;

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 堆 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

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

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

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

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

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

解题思路

方法一:哈希表 + 有序集合

我们定义以下几个数据结构或变量,其中:

  • d:表示一个哈希表,用于存储时间戳和对应的价格;
  • ls:表示一个有序集合,用于存储所有的价格;
  • last:表示最后一次更新的时间戳。

那么,我们可以得到以下几个操作:

  • update(timestamp, price):更新时间戳 timestamp 对应的价格为 price。如果 timestamp 已经存在,那么我们需要先将其对应的价格从有序集合中删除,再将其更新为 price。否则,我们直接将其更新为 price。然后,我们需要更新 lastmax(last, timestamp)。时间复杂度为 O(logn)O(\log n)
  • current():返回 last 对应的价格。时间复杂度为 O(1)O(1)
  • maximum():返回有序集合中的最大值。时间复杂度为 O(logn)O(\log n)
  • minimum():返回有序集合中的最小值。时间复杂度为 O(logn)O(\log n)

空间复杂度为 O(n)O(n)。其中,nnupdate 操作的次数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class StockPrice:
    def __init__(self):
        self.d = {}
        self.ls = SortedList()
        self.last = 0

    def update(self, timestamp: int, price: int) -> None:
        if timestamp in self.d:
            self.ls.remove(self.d[timestamp])
        self.d[timestamp] = price
        self.ls.add(price)
        self.last = max(self.last, timestamp)

    def current(self) -> int:
        return self.d[self.last]

    def maximum(self) -> int:
        return self.ls[-1]

    def minimum(self) -> int:
        return self.ls[0]


# Your StockPrice object will be instantiated and called as such:
# obj = StockPrice()
# obj.update(timestamp,price)
# param_2 = obj.current()
# param_3 = obj.maximum()
# param_4 = obj.minimum()
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Understanding how to efficiently handle unordered data streams.

  • question_mark

    Familiarity with data structures like hash tables, heaps, and ordered sets.

  • question_mark

    Ability to design solutions that can handle data corrections without reordering the entire dataset.

warning

常见陷阱

外企场景
  • error

    Failing to account for efficient price correction handling, which could lead to incorrect maximum or minimum results.

  • error

    Not considering the potential impact of unordered data, causing slow updates and queries.

  • error

    Overcomplicating the solution with unnecessary structures, leading to poor performance.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if we have a large number of queries and need to optimize for multiple updates and queries at once?

  • arrow_right_alt

    How would the solution change if the data were to be processed offline (all queries given at once)?

  • arrow_right_alt

    What if the price updates could be negative or zero, requiring extra validation?

help

常见问题

外企场景

股票价格波动题解:堆 | LeetCode #2034 中等