Freezer Warmest Stretch
A restaurant's walk-in freezer logs its temperature once a minute, in °C. A food-safety rule cares about the warmest reading in every stretch of k consecutive minutes, since one warm spike matters even if the average looks fine. The inspector wants that peak for every k-minute stretch, over logs that cover whole weeks, so the report must be fast.
Your task
Write warmestStretches(temps, k). Return a list with one entry for each window of k consecutive readings, in order from the start: the largest value in that window. A log of n readings has n - k + 1 windows.
Aim for O(n) time in total, no matter how large k is.
warmestStretches(temps: int[], k: int) → int[]
Examples
temps = [-18, -16, -19, -20, -17, -21] k = 3
[-16, -16, -17, -17]
[-18, -16, -19] peaks at -16, and so does [-16, -19, -20]. Then -16 leaves: [-19, -20, -17] and [-20, -17, -21] peak at -17.
temps = [-6, -9, -12] k = 1
[-6, -9, -12]
With k = 1 each window is one reading, so the report is the log itself.
Constraints
- 1 ≤ temps.length ≤ 100,000
- 1 ≤ k ≤ temps.length
- -40 ≤ temps[i] ≤ 40
Start with brute force
Scan each of the n - k + 1 windows for its maximum: O(n·k). With 100,000 readings and k = 50,000, that is 50,001 windows of 50,000 readings, about 2.5 billion comparisons, though neighbouring windows share all but one reading.
Hints
0 of 4 shown- Hint 1
In the window
[-19, -20, -17], -17 arrived last. Can -19 or -20 ever be the warmest of a later window that also contains -17? - Hint 2
A reading colder than (or equal to) a newer reading can never be the peak again: the newer one is warmer and stays at least as long. Throw it away for good.
- Hint 3
Keep only readings that could still be a peak, in arrival order. They are then falling, so the front is the peak. You add at the back, drop useless readings from the back, and drop expired ones from the front. Which structure does all three in O(1)?
- Hint 4
Use a deque of indexes. For each minute
i: pop from the back whiletemps[back] <= temps[i], pushi, then pop the front if it isi - kor older. Oncei >= k - 1, recordtemps[front].
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
When a warmer reading arrives, every older reading not warmer than it is useless: the new one beats it and stays longer. Throw those away. What remains, in arrival order, is falling, so the front is the peak. Readings enter at the back and can leave from either end, so a deque is the right structure. Store indexes instead of temperatures so you can tell when the front reading has left the window.
Trace [-18, -16, -19, -20, -17, -21] with k = 3 (deque as index(temp), front first):
- i = 0 (-18). Push it, so the deque is 0(-18).
- i = 1 (-16). Since -18 ≤ -16, drop index 0, then push 1. The deque is 1(-16).
- i = 2 (-19). It is colder, so keep both. The deque is 1(-16), 2(-19). This is the first full window, and its peak is -16.
- i = 3 (-20). Push it, so the deque is 1(-16), 2(-19), 3(-20). Front index 1 is still inside the window (indexes 1 to 3), so the peak is -16.
- i = 4 (-17). Drop indexes 3 and 2 from the back because both are colder, then push 4. The deque is 1(-16), 4(-17). The front index 1 equals 4 - 3, so it has expired and is dropped. The peak is -17.
- i = 5 (-21). Push it, so the deque is 4(-17), 5(-21), and the peak is -17.
In each step, clean the back first, then push, then check the front. Checking the front against i - k keeps exactly the last k minutes.
function warmestStretches(temps, k) {
const dq = []; // indexes; their temps fall from front to back
let head = 0; // front of the deque (avoids slow shift())
const peaks = [];
for (let i = 0; i < temps.length; i++) {
// drop readings that can never be a peak again
while (dq.length > head && temps[dq[dq.length - 1]] <= temps[i]) dq.pop();
dq.push(i);
if (dq[head] <= i - k) head++; // the front reading left the window
if (i >= k - 1) peaks.push(temps[dq[head]]);
}
return peaks;
}from collections import deque
def warmest_stretches(temps, k):
dq = deque() # indexes; their temps fall from front to back
peaks = []
for i, t in enumerate(temps):
# drop readings that can never be a peak again
while dq and temps[dq[-1]] <= t:
dq.pop()
dq.append(i)
if dq[0] <= i - k: # the front reading left the window
dq.popleft()
if i >= k - 1:
peaks.append(temps[dq[0]])
return peaksRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.