504. Longest Steady Temperature Stretch
Longest Steady Temperature Stretch

A freezer in a cold-storage warehouse has a sensor that records its temperature once every minute. The readings are stored in time order.

A steady stretch is a group of back-to-back minutes that all have the same temperature. Only neighbouring minutes count, so if a reading comes back after a different reading, it starts a new stretch.

Return the number of minutes in the longest steady stretch. If there are no readings, return 0.

Method Signature

int longestSteadyStretch(List<Integer> temperatures)
  • temperatures.get(i) is the temperature at minute i.
  • Returns the length of the longest steady stretch.

Constraints

  • 0 ≤ temperatures.size() ≤ 100,000
  • -500 ≤ temperatures.get(i) ≤ 1000

Examples

Example 1

longestSteadyStretch(temperatures = [-12, -12, -10, -10, -10, -10, -12])

Output: 4

The steady stretches are [-12, -12], [-10, -10, -10, -10] and [-12]. The longest one is -10 for 4 minutes.

Example 2

longestSteadyStretch(temperatures = [-25, -25, -25])

Output: 3

The temperature never changes, so all 3 minutes form one stretch.

Example 3

longestSteadyStretch(temperatures = [-8, -9, -8])

Output: 1

No two neighbouring minutes have the same temperature, so every stretch lasts 1 minute.

Example 4

longestSteadyStretch(temperatures = [-14, -14, -16, -14, -14, -14])

Output: 3

-14 appears 5 times, but -16 splits it into two stretches of 2 and 3 minutes. The longer one has 3 minutes.

Example 5

longestSteadyStretch(temperatures = [])

Output: 0

There are no readings, so the answer is 0.



Please use Laptop/Desktop or any other large screen to add/edit code.