|
| 1 | +/** |
| 2 | + * 2034. Stock Price Fluctuation |
| 3 | + * https://leetcode.com/problems/stock-price-fluctuation/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are given a stream of records about a particular stock. Each record contains a timestamp |
| 7 | + * and the corresponding price of the stock at that timestamp. |
| 8 | + * |
| 9 | + * Unfortunately due to the volatile nature of the stock market, the records do not come in order. |
| 10 | + * Even worse, some records may be incorrect. Another record with the same timestamp may appear |
| 11 | + * later in the stream correcting the price of the previous wrong record. |
| 12 | + * |
| 13 | + * Design an algorithm that: |
| 14 | + * - Updates the price of the stock at a particular timestamp, correcting the price from any |
| 15 | + * previous records at the timestamp. |
| 16 | + * - Finds the latest price of the stock based on the current records. The latest price is the |
| 17 | + * price at the latest timestamp recorded. |
| 18 | + * - Finds the maximum price the stock has been based on the current records. |
| 19 | + * - Finds the minimum price the stock has been based on the current records. |
| 20 | + * |
| 21 | + * Implement the StockPrice class: |
| 22 | + * - StockPrice() Initializes the object with no price records. |
| 23 | + * - void update(int timestamp, int price) Updates the price of the stock at the given timestamp. |
| 24 | + * - int current() Returns the latest price of the stock. |
| 25 | + * - int maximum() Returns the maximum price of the stock. |
| 26 | + * - int minimum() Returns the minimum price of the stock. |
| 27 | + */ |
| 28 | + |
| 29 | +var StockPrice = function() { |
| 30 | + this.timestamps = new Map(); |
| 31 | + this.highestTimestamp = 0; |
| 32 | + this.minHeap = new PriorityQueue((a, b) => a[0] - b[0]); |
| 33 | + this.maxHeap = new PriorityQueue((a, b) => b[0] - a[0]); |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * @param {number} timestamp |
| 38 | + * @param {number} price |
| 39 | + * @return {void} |
| 40 | + */ |
| 41 | +StockPrice.prototype.update = function(timestamp, price) { |
| 42 | + this.timestamps.set(timestamp, price); |
| 43 | + this.highestTimestamp = Math.max(this.highestTimestamp, timestamp); |
| 44 | + |
| 45 | + this.minHeap.enqueue([price, timestamp]); |
| 46 | + this.maxHeap.enqueue([price, timestamp]); |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * @return {number} |
| 51 | + */ |
| 52 | +StockPrice.prototype.current = function() { |
| 53 | + return this.timestamps.get(this.highestTimestamp); |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * @return {number} |
| 58 | + */ |
| 59 | +StockPrice.prototype.maximum = function() { |
| 60 | + let [currPrice, timestamp] = this.maxHeap.dequeue(); |
| 61 | + |
| 62 | + while (currPrice !== this.timestamps.get(timestamp)) { |
| 63 | + [currPrice, timestamp] = this.maxHeap.dequeue(); |
| 64 | + } |
| 65 | + |
| 66 | + this.maxHeap.enqueue([currPrice, timestamp]); |
| 67 | + return currPrice; |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * @return {number} |
| 72 | + */ |
| 73 | +StockPrice.prototype.minimum = function() { |
| 74 | + let [currPrice, timestamp] = this.minHeap.dequeue(); |
| 75 | + |
| 76 | + while (currPrice !== this.timestamps.get(timestamp)) { |
| 77 | + [currPrice, timestamp] = this.minHeap.dequeue(); |
| 78 | + } |
| 79 | + |
| 80 | + this.minHeap.enqueue([currPrice, timestamp]); |
| 81 | + return currPrice; |
| 82 | +}; |
0 commit comments