You are given an array of integers, nums. A rising run is a block of neighbouring positions where every number is strictly bigger than the number just before it. A single number on its own counts as a run of length 1.
Return the length of the longest rising run in nums. The numbers must sit next to each other: you cannot skip over a smaller number to keep a run going, and equal neighbours break it.
Example 1
Input: nums = [4, 7, 9, 3, 5, 8, 10, 2] Output: 4 Why: 3, 5, 8, 10 rise one after another
2 more examples and the constraints
Example 2
Input: nums = [6, 6, 6] Output: 1 Why: equal neighbours do not count as rising
Example 3
Input: nums = [8, 5, 3, 1] Output: 1 Why: every step goes down, so each run is one number
Constraints
- 1 ≤ length of nums ≤ 10,000
- Every number is between −1,000,000,000 and 1,000,000,000
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Extend from every startO(n²)
- One pass with a run counterO(n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 10,000 rising numbers | Verdict |
|---|---|---|---|
| Extend from every start | O(n²) | Fine to mention, wasteful | |
| One pass with a run counter | O(n) | Best; one look per number |
n = length of nums.
Edge cases to test
- One number. [7] has a run of length 1.
- Equal neighbours. [6, 6, 6] gives 1, because the rule is strictly bigger.
- Longest run at the end. [5, 1, 2, 3] gives 3; check best inside the loop, not only when a run breaks.
- Whole list rises. [1, 2, 3, 4] gives 4, the full length.
Common mistakes
- Updating best only when a run breaks, so a run that reaches the last index is never counted.
- Using >= instead of >, which lets equal neighbours extend a run.
- Confusing this with the longest increasing subsequence, which may skip numbers.
How to talk through it
- 1Clarify. Must the numbers be next to each other? Does equal count as rising?
- 2Give the brute force. Extend a run from every start; say it is O(n²) on a list that rises all the way.
- 3Spot the waste. A run only breaks where a number fails to beat its left neighbour, so one pass can track the current run.
- 4Code the counter. Grow or reset run, then update best on every index.
- 5Test out loud. Run a single number, equal neighbours and a run that ends at the last index.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?