Official

A - 連続晴天の最長記録 / Longest Streak of Hot Days Editorial by admin

Qwen3-Coder-480B

Overview

Given daily temperature data, find the longest number of consecutive days where the maximum temperature is \(K\) degrees or higher (extreme heat days).

Analysis

This problem can be solved by simply determining whether each day is an extreme heat day while counting the length of consecutive intervals. For example, consider the following input:

N = 7, K = 30
A = [29, 31, 32, 28, 30, 31, 32]

In this case, the extreme heat days are from day \(2\) to day \(3\), and from day \(5\) to day \(7\). The lengths of these consecutive intervals are \(2\) and \(3\) respectively, so the answer is \(3\).

A naive approach would be to check all intervals for consecutive extreme heat days, but this would require up to \(O(N^2)\) computation, which is too slow for the constraint \(N \leq 10^6\).

Instead, we use a linear scan approach that maintains the current consecutive interval length while examining elements one by one. This allows us to check each element only once, keeping the computation efficient.

Algorithm

  1. max_length: Records the maximum length of consecutive intervals found so far.
  2. current_length: Records the length of the consecutive interval at the current position.
  3. For each day \(i\), perform the following:
    • If \(A[i] \geq K\), increment current_length and update max_length.
    • Otherwise (i.e., it is not an extreme heat day), reset current_length to \(0\).
  4. Finally, output max_length.

This algorithm finds the answer by scanning the array from the beginning only once.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(1)\) (excluding the input array)

Implementation Notes

  • Don’t forget to reset current_length when encountering a non-extreme heat day.

  • It is safest to update the maximum value every time an extreme heat day is encountered.

  • If there are no extreme heat days at all, 0 is automatically output, so no special handling is needed.

    Source Code

N, K = map(int, input().split())
A = list(map(int, input().split()))

max_length = 0
current_length = 0

for i in range(N):
    if A[i] >= K:
        current_length += 1
        max_length = max(max_length, current_length)
    else:
        current_length = 0

print(max_length)

This editorial was generated by qwen3-coder-480b.

posted:
last update: