Problem 237441 · easy · Phase 02 Linear Data Structures

Longest Stable Stretch

arrays · iteration · runs

A temperature sensor reports one reading per minute in readings. A stable stretch is a maximal block of consecutive readings that are all identical. Return the length of the longest stable stretch, or 0 if there are no readings.

Examples

Input:  readings = [4, 4, 7, 7, 7, 4]
Output: 3
Explanation: The block 7, 7, 7 is the longest.

Input:  readings = [1, 2, 3]
Output: 1

Constraints

  • 0 <= len(readings) <= 10**5
  • Target: O(n) time, O(1) extra space.

Goals

  • Track the length of the current run of equal neighbours
  • Reset the run counter when the value changes
  • Handle the empty list
Starting Python…