1
var StockPrice = function () {3
// Should simply store [timestamp -> value]7
// Should simply store timestamp as value, and price as priority8
this.max = new MaxPriorityQueue();10
// Initialize Min Heap11
// Should simply store timestamp as value, and price as priority12
this.min = new MinPriorityQueue();14
// This is find maximum timestamp i.e. current stock price.15
this.currentTimestamp = -1;19
* @param {number} timestamp20
* @param {number} price23
StockPrice.prototype.update = function (timestamp, price) {24
// Check if its a new timestamp or an update to older25
this.currentTimestamp = Math.max(this.currentTimestamp, timestamp);27
// Upsert [Insert or Update value] for price at timestamp28
this.data.set(timestamp, price);30
// insert timestamp to the queue with price is priority31
this.max.enqueue(timestamp, price);32
this.min.enqueue(timestamp, price);38
StockPrice.prototype.current = function () {39
// get the data with max timestamp from map40
return this.data.get(this.currentTimestamp);46
StockPrice.prototype.maximum = function () {47
// check the front of max queue48
// element -> timestamp50
let { element, priority } = this.max.front();52
// if priority [i.e. price] is not same latest value, discard it53
while (priority != this.data.get(element)) {56
element = this.max.front().element;57
priority = this.max.front().priority;67
StockPrice.prototype.minimum = function () {68
// check the front of max queue69
// element -> timestamp71
let { element, priority } = this.min.front();73
// if priority [i.e. price] is not same latest value, discard it74
while (priority != this.data.get(element)) {77
element = this.min.front().element;78
priority = this.min.front().priority;86
* Your StockPrice object will be instantiated and called as such:87
* var obj = new StockPrice()88
* obj.update(timestamp,price)89
* var param_2 = obj.current()90
* var param_3 = obj.maximum()91
* var param_4 = obj.minimum()