A cold-storage facility records one integer temperature reading at a time.
Design a monitor that can quickly report the highest temperature among a fixed number of the most recently recorded readings.
ColdStorageMonitor
ColdStorageMonitor(int recentReadingLimit)
recentReadingLimit is the maximum number of recent readings considered when finding the highest temperature.void recordTemperature(int temperature)
Records a new temperature reading.
int getHighestRecentTemperature()
Returns the highest temperature among the most recent readings.
recentReadingLimit readings are considered.getHighestRecentTemperature does not change the recorded readings.1 ≤ recentReadingLimit ≤ 100,000-1,000,000,000 ≤ temperature ≤ 1,000,000,000100,000 method calls will be made.getHighestRecentTemperature will be called only after at least one temperature has been recorded.recordTemperature should run in O(1) amortized time.getHighestRecentTemperature should run in O(1) time.O(recentReadingLimit) space.ColdStorageMonitor(recentReadingLimit = 3)
recordTemperature(temperature = 18)
recordTemperature(temperature = 24)
recordTemperature(temperature = 21)
getHighestRecentTemperature() returns 24.
recordTemperature(temperature = 16)
getHighestRecentTemperature() returns 24 because the three most recent readings are 24, 21, and 16.
recordTemperature(temperature = 29)
getHighestRecentTemperature() returns 29 because the three most recent readings are 21, 16, and 29.
ColdStorageMonitor(recentReadingLimit = 2)
recordTemperature(temperature = -8)
recordTemperature(temperature = -3)
getHighestRecentTemperature() returns -3.
recordTemperature(temperature = -10)
getHighestRecentTemperature() returns -3 because the two most recent readings are -3 and -10.
recordTemperature(temperature = -12)
getHighestRecentTemperature() returns -10 because the two most recent readings are -10 and -12.