Skip to content

Commit d19a16e

Browse files
committed
Add solution #2034
1 parent e437f0f commit d19a16e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,7 @@
18441844
2031|[Count Subarrays With More Ones Than Zeros](./solutions/2031-count-subarrays-with-more-ones-than-zeros.js)|Medium|
18451845
2032|[Two Out of Three](./solutions/2032-two-out-of-three.js)|Easy|
18461846
2033|[Minimum Operations to Make a Uni-Value Grid](./solutions/2033-minimum-operations-to-make-a-uni-value-grid.js)|Medium|
1847+
2034|[Stock Price Fluctuation](./solutions/2034-stock-price-fluctuation.js)|Medium|
18471848
2035|[Partition Array Into Two Arrays to Minimize Sum Difference](./solutions/2035-partition-array-into-two-arrays-to-minimize-sum-difference.js)|Hard|
18481849
2036|[Maximum Alternating Subarray Sum](./solutions/2036-maximum-alternating-subarray-sum.js)|Medium|
18491850
2037|[Minimum Number of Moves to Seat Everyone](./solutions/2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy