Count Strictly Rising Signal Windows
Arrays & HashingAuto-graded · 21 test casesLast updated
Count how many contiguous subarrays of length exactly k have strictly increasing values in a signal array.
You are monitoring a signal over n time steps. You are given:
- An integer
n, the total number of readings. - An integer
k(1 ≤ k ≤ n), the required window length. - An integer array
signalof lengthn, wheresignal[i]is the signal strength at time stepi(0-based).
A window signal[l..r] (0 ≤ l ≤ r < n) is strictly rising if for every index i with l ≤ i < r, the value signal[i] < signal[i + 1].
Count the number of windows of length exactly k that are strictly rising. Formally, count the number of starting indices s such that 0 ≤ s ≤ n − k and signal[s] < signal[s + 1] < … < signal[s + k − 1].
Return a single integer: the count of strictly rising windows of length k.
Constraints:
- 1 ≤ k ≤ n
- 1 ≤ n ≤ 2 × 10⁵
- Each element of
signalis an integer (may be negative, zero, or positive). - Equal adjacent values break a strictly rising run.
- When k = 1, every single-element window is trivially strictly rising, so the answer is n.
Examples
Example 1
Input: count_rising_windows(5, 3, [1, 2, 3, 4, 5])
Output: 3
Example 2
Input: count_rising_windows(4, 2, [1, 2, 1, 2])
Output: 2
Example 3
Input: count_rising_windows(4, 1, [4, 3, 2, 1])
Output: 4
Hints
Hint 1
Instead of checking every window from scratch, track the length of the current strictly increasing streak as you scan left to right.
Hint 2
When k is 1, every single element qualifies on its own, so handle that as a special case returning n.
Hint 3
A length-k window ending at index i is valid exactly when the increasing streak ending at i has length at least k, so increment your count each time the streak reaches k.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Sweep through the array once while maintaining run, the length of the current strictly increasing streak ending at the current index. At each position i (starting from index 1), compare signal[i-1] and signal[i]: if the former is smaller, extend the streak; otherwise reset it to 1. Whenever the streak length reaches k, the window of length k ending at i is strictly rising, so increment the count.
The case k = 1 needs special handling because every single element is trivially strictly increasing, giving an answer of n. The main loop starts at index 1 and never considers index 0 as a right endpoint, so without the special case a length-1 array would return 0 instead of 1.
Why it is correct
A window of length k ending at index i is strictly increasing if and only if the strictly increasing streak ending at i has length at least k. Each valid window has exactly one right endpoint, so every window is counted exactly once and no double-counting occurs. Equal adjacent values reset the streak, which correctly reflects the strict inequality requirement.
Complexity
- Time: O(n) — one pass through the array with constant work per element.
- Space: O(1) beyond the input array.
Reference solution
from typing import List
def count_rising_windows(n: int, k: int, signal: List[int]) -> int:
if k == 1:
return n
count = 0
run = 1 # length of the current strictly increasing streak ending at index i
for i in range(1, n):
if signal[i - 1] < signal[i]:
run += 1
else:
run = 1
if run >= k:
count += 1
return count