-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path2034-stock-price-fluctuation.js
62 lines (55 loc) · 1.46 KB
/
2034-stock-price-fluctuation.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const StockPrice = function () {
this.timeToPrice = new Map()
this.lastTime = 0
this.minPrices = new MinPriorityQueue({ priority: (stock) => stock.price })
this.maxPrices = new MaxPriorityQueue({ priority: (stock) => stock.price })
}
/**
* @param {number} timestamp
* @param {number} price
* @return {void}
*/
StockPrice.prototype.update = function (timestamp, price) {
this.timeToPrice.set(timestamp, price)
this.lastTime = Math.max(this.lastTime, timestamp)
this.minPrices.enqueue({ timestamp, price })
this.maxPrices.enqueue({ timestamp, price })
}
/**
* @return {number}
*/
StockPrice.prototype.current = function () {
return this.timeToPrice.get(this.lastTime)
}
/**
* @return {number}
*/
StockPrice.prototype.maximum = function () {
while (
this.maxPrices.front().element.price !==
this.timeToPrice.get(this.maxPrices.front().element.timestamp)
) {
this.maxPrices.dequeue()
}
return this.maxPrices.front().element.price
}
/**
* @return {number}
*/
StockPrice.prototype.minimum = function () {
while (
this.minPrices.front().element.price !==
this.timeToPrice.get(this.minPrices.front().element.timestamp)
) {
this.minPrices.dequeue()
}
return this.minPrices.front().element.price
}
/**
* Your StockPrice object will be instantiated and called as such:
* var obj = new StockPrice()
* obj.update(timestamp,price)
* var param_2 = obj.current()
* var param_3 = obj.maximum()
* var param_4 = obj.minimum()
*/