LeetCode in Kotlin

2034. Stock Price Fluctuation

Medium

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:

Implement the StockPrice class:

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:

Solution

import java.util.PriorityQueue

class StockPrice {
    private class Record(var time: Int, var price: Int)

    private val map: MutableMap<Int, Int>
    private val maxHeap: PriorityQueue<Record>
    private val minHeap: PriorityQueue<Record>
    private var latestTimestamp = 0

    init {
        map = HashMap()
        maxHeap = PriorityQueue { r1: Record, r2: Record -> Integer.compare(r2.price, r1.price) }
        minHeap = PriorityQueue { r1: Record, r2: Record -> Integer.compare(r1.price, r2.price) }
    }

    fun update(timestamp: Int, price: Int) {
        latestTimestamp = Math.max(timestamp, latestTimestamp)
        maxHeap.offer(Record(timestamp, price))
        minHeap.offer(Record(timestamp, price))
        map[timestamp] = price
    }

    fun current(): Int {
        return map[latestTimestamp]!!
    }

    fun maximum(): Int {
        while (true) {
            val rec = maxHeap.peek()
            if (map[rec.time] == rec.price) {
                return rec.price
            }
            maxHeap.poll()
        }
    }

    fun minimum(): Int {
        while (true) {
            val rec = minHeap.peek()
            if (map[rec.time] == rec.price) {
                return rec.price
            }
            minHeap.poll()
        }
    }
}
/*
 * Your StockPrice object will be instantiated and called as such:
 * var obj = StockPrice()
 * obj.update(timestamp,price)
 * var param_2 = obj.current()
 * var param_3 = obj.maximum()
 * var param_4 = obj.minimum()
 */