Longest Strictly Rising Streak in a Row of Readings
Arrays & HashingAuto-graded · 23 test casesLast updated
Scan an integer list once to find the longest block of adjacent, strictly rising values, handling empty input, equal neighbors, and runs that reach the end.
Longest Rising Streak
You are given a list of integers readings. A streak is a block of consecutive positions in the list in which every value is strictly larger than the value just before it.
More formally, the positions i, i+1, ..., j form a streak if readings[k] < readings[k+1] for every k with i <= k < j. A block containing a single element is always a streak of length 1.
Return the length of the longest streak in readings.
Rules
- The elements must be adjacent in the list. Skipping positions is not allowed.
- Two equal neighbors do not count as rising, so they end the current streak.
- If
readingsis empty, return0. - If
readingsis non-empty, the answer is at least1.
Constraints
0 <= len(readings) <= 10^5-10^9 <= readings[i] <= 10^9- Aim for
O(n)time andO(1)extra space.
Examples
Example 1
Input: longest_rising_streak([1, 3, 5, 4, 7])
Output: 3
Example 2
Input: longest_rising_streak([2, 2, 2, 2, 2])
Output: 1
Example 3
Input: longest_rising_streak([])
Output: 0
Hints
Hint 1
Because the elements must be adjacent, each element either extends the streak that ended just before it or begins a brand new one.
Hint 2
Keep two numbers while scanning left to right: the length of the streak ending at the current position, and the best length seen so far.
Hint 3
Compare using strict greater-than so equal neighbors reset the streak, and return 0 right away for an empty list.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since a streak must be made of adjacent elements, each element has only two possibilities: it continues the streak that ended at the previous position, or it starts a new streak of length 1. A single left-to-right pass is therefore enough.
Maintain two values:
current: the length of the streak that ends at the current index.best: the largestcurrentseen so far.
For each index i from 1 onward:
- If
readings[i] > readings[i - 1], incrementcurrent. - Otherwise (smaller or equal), set
current = 1. - Update
bestifcurrentexceeds it.
An empty list returns 0 before the loop starts.
Why it is correct
After processing index i, current equals the length of the longest strictly rising block that ends exactly at i. This holds at index 0, where current = 1. For later indices, a block of length 2 or more ending at i must include i - 1 and require readings[i-1] < readings[i]. In that case the longest such block is the longest one ending at i - 1 extended by one element. Otherwise only the single element qualifies.
Every block ends at some index, so the maximum of current over all indices is the answer. Updating best inside the loop ensures a streak that reaches the end of the list is counted. Using strict > ensures equal neighbors break the streak.
Complexity
- Time:
O(n), one pass with constant work per element. - Space:
O(1), only two counters.
Reference solution
from typing import List
def longest_rising_streak(readings: List[int]) -> int:
if not readings:
return 0
best = 1
current = 1
for i in range(1, len(readings)):
if readings[i] > readings[i - 1]:
current += 1
else:
current = 1
if current > best:
best = current
return best