2034. Stock Price Fluctuation

Stock Price Fluctuation (LeetCode 2034) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

How to solve Stock Price Fluctuation

Stock Price Fluctuation is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. How would you solve the problem for offline queries (all queries given at once)?
  2. Think about which data structure can help insert and delete the most optimal way.

The reference solution runs in O(log N) time and O(N) space.

The Stock Price Fluctuation solution and editorial walks through the full approach with reference code.

Problem statement

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 update, current, maximum, and minimum.

- current, maximum, and minimum will be called only after update has been called at least once.

Topics and companies

Topics: Hash Table, Design, Heap (Priority Queue), Data Stream, Ordered Set.

How to practise Stock Price Fluctuation on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(log N) time and O(N) space.

Open Stock Price Fluctuation in the code editor, or read the Stock Price Fluctuation editorial for a worked solution with its approach and complexity analysis.

Where Stock Price Fluctuation sits in the DSA pattern sheet

Problems related to Stock Price Fluctuation

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Stock Price Fluctuation FAQ

What is the LeetCode number of Stock Price Fluctuation?

Stock Price Fluctuation is LeetCode problem 2034, rated Medium.

What pattern is used to solve Stock Price Fluctuation?

Stock Price Fluctuation is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Stock Price Fluctuation?

The reference solution runs in O(log N) time and O(N) space.

Preparing this workspace.