ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 2034. Stock Price Fluctuation

2022-08-03 08:34:40  阅读:176  来源: 互联网

标签:int Fluctuation Price maximum update records 2034 timestamp price


You are given a stream of records about a particular stock. Each record contains a timestamp and the corresponding price of the stock at that timestamp.

Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect. Another record with the same timestamp may appear later in the stream correcting the price of the previous wrong record.

Design an algorithm that:

  • Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp.
  • Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded.
  • Finds the maximum price the stock has been based on the current records.
  • Finds the minimum price the stock has been based on the current records.

Implement the StockPrice class:

  • StockPrice() Initializes the object with no price records.
  • void update(int timestamp, int price) Updates the price of the stock at the given timestamp.
  • int current() Returns the latest price of the stock.
  • int maximum() Returns the maximum price of the stock.
  • int minimum() Returns the minimum price of the stock.

Example 1:

Input
["StockPrice", "update", "update", "current", "maximum", "update", "maximum", "update", "minimum"]
[[], [1, 10], [2, 5], [], [], [1, 3], [], [4, 2], []]
Output
[null, null, null, 5, 10, null, 5, null, 2]

Explanation
StockPrice stockPrice = new StockPrice();
stockPrice.update(1, 10); // Timestamps are [1] with corresponding prices [10].
stockPrice.update(2, 5);  // Timestamps are [1,2] with corresponding prices [10,5].
stockPrice.current();     // return 5, the latest timestamp is 2 with the price being 5.
stockPrice.maximum();     // return 10, the maximum price is 10 at timestamp 1.
stockPrice.update(1, 3);  // The previous timestamp 1 had the wrong price, so it is updated to 3.
                          // Timestamps are [1,2] with corresponding prices [3,5].
stockPrice.maximum();     // return 5, the maximum price is 5 after the correction.
stockPrice.update(4, 2);  // Timestamps are [1,2,4] with corresponding prices [3,5,2].
stockPrice.minimum();     // return 2, the minimum price is 2 at timestamp 4.

Constraints:

  • 1 <= timestamp, price <= 109
  • At most 105 calls will be made in total to updatecurrentmaximum, and minimum.
  • currentmaximum, and minimum will be called only after update has been called at least once.

股票价格波动。

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

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

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

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

StockPrice() 初始化对象,当前无股票价格记录。
void update(int timestamp, int price) 在时间点 timestamp 更新股票价格为 price 。
int current() 返回股票 最新价格 。
int maximum() 返回股票 最高价格 。
int minimum() 返回股票 最低价格 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/stock-price-fluctuation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道设计题。题意不难懂,我直接讲思路。

这道题我们需要两个treemap,records 记录<时间戳,价格>,vals <价格,这个价格涉及到的所有时间戳>。尤其第二个 treemap 比较巧妙,这里他记录的是同一个价格,一共出现过几次。

注意题目要求实现的几个函数,

  • update() 很好理解,找到一个之前的 timestamp 并更新价格。但是注意我们需要把错误的价格 prevPrice 从第二个 treemap 里删除,并且如果这个价格只出现过一次的话,要把这个价格从 vals 里删除。
  • current() - 因为 records 作为一个 treemap,他的 key 是有序的,所以我们直接返回最后一个 key 背后对应的 value即可,这就是当前的股价
  • maximum() - 因为 vals 是 treemap,所以返回 vals 里最后一个key
  • minimum() - 因为 vals 是 treemap,所以返回 vals 里第一个key

时间O(nlogn) - treemap的读取操作是O(1),但是内部的排序需要O(nlogn)

空间O(n)

Java实现

 1 class StockPrice {
 2     TreeMap<Integer, Integer> records;      // <timestamp, price>
 3     TreeMap<Integer, Set<Integer>> vals;    // <price, set of timestamp>
 4 
 5     public StockPrice() {
 6         records = new TreeMap<>();
 7         vals = new TreeMap<>();
 8     }
 9     
10     public void update(int timestamp, int price) {
11         if (records.containsKey(timestamp)) {
12             int prevPrice = records.get(timestamp);
13             Set<Integer> set = vals.get(prevPrice);
14             set.remove(timestamp);
15             if (set.size() == 0) {
16                 vals.remove(prevPrice);
17             }
18         }
19         records.put(timestamp, price);
20         vals.putIfAbsent(price, new HashSet<>());
21         vals.get(price).add(timestamp);
22     }
23     
24     public int current() {
25         return records.lastEntry().getValue();
26     }
27     
28     public int maximum() {
29         return vals.lastKey();
30     }
31     
32     public int minimum() {
33         return vals.firstKey();
34     }
35 }
36 
37 /**
38  * Your StockPrice object will be instantiated and called as such:
39  * StockPrice obj = new StockPrice();
40  * obj.update(timestamp,price);
41  * int param_2 = obj.current();
42  * int param_3 = obj.maximum();
43  * int param_4 = obj.minimum();
44  */

 

相关题目

981. Time Based Key-Value Store

2034. Stock Price Fluctuation

LeetCode 题目总结

标签:int,Fluctuation,Price,maximum,update,records,2034,timestamp,price
来源: https://www.cnblogs.com/cnoodle/p/16545753.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有