41. Uphill Run

A hiker records the altitude at each checkpoint of a trail as the array heights. An uphill run is a block of consecutive checkpoints where every altitude is strictly higher than the one before it. A single checkpoint on its own counts as a run of length 1.

Return the length of the longest uphill run in heights.

Equal altitudes break a run, and so does any drop. Your solution should make one pass over the array and use only constant extra memory.

Example 1

Input:
heights = [3,5,6,2,4,7,9,1]
Output:
4
Explanation:

The run 2, 4, 7, 9 has length 4, longer than 3, 5, 6.

Example 2

Input:
heights = [5,5,5]
Output:
1
Explanation:

No altitude is higher than the previous one, so every run has length 1.

Constraints

1 ≤ heights.length ≤ 105

-109 ≤ heights[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…