Problem 335454 · easy · Phase 03 Linear Management & Searching

Running Temperature Spread

prefix max · prefix min · running extremum

temps[i] is the temperature recorded at hour i. The spread so far at hour i is the difference between the highest and the lowest temperature recorded in hours 0..i. Return the list of spreads for every hour.

Examples

Input:  temps = [4, 7, 2, 9]
Output: [0, 3, 5, 7]
Explanation: hour 2 has seen 7 and 2, so the spread is 5; hour 3 has seen 9 and 2.

Input:  temps = [5, 5, 5]
Output: [0, 0, 0]

Constraints

  • 1 <= len(temps) <= 10**5
  • -10**6 <= temps[i] <= 10**6
  • Target complexity: O(n) time. Recomputing max and min of a growing prefix each hour is too slow.

Goals

  • Maintain both a running maximum and a running minimum
  • Produce one value per position in a single pass
Starting Python…