Longest Climbing Stretch
EasySliding Window
Problem
A hiker's GPS records altitudes nums at regular intervals. A climbing stretch is a run of consecutive readings where each one is strictly higher than the previous.
Return the length of the longest climbing stretch. A single reading counts as a stretch of length 1; an empty log has answer 0.
Example 1
- Input:
- nums = [2, 5, 7, 7, 1, 3, 4, 8]
- Output:
- 4
[1, 3, 4, 8] climbs four readings in a row. [2, 5, 7] stops at the repeated 7.
Example 2
- Input:
- nums = [6, 6, 6]
- Output:
- 1
Equal readings are not a climb.
Constraints
- 0 ≤ nums.length ≤ 10^5
- -10^9 ≤ nums[i] ≤ 10^9
Hint
Show the first hint
Where can a climbing stretch start?
Walkthrough
How to approach it
01Clarify before coding
- Strictly increasing? Yes, equal neighbours break the stretch.
- Contiguous? Yes, this is not the longest increasing subsequence.
- Empty input? Return 0.
02Brute force
For every start, extend while the next value is larger, keeping the longest.
Cost: O(n²) in the worst case (a fully increasing array re-scanned from every start).
The rest of the walkthrough is in the app
The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.
This problem is part of Pro.