Longest Mountain in Array
Real-world version: Longest Ridge in the ProfileThe statement here is our own: the same inputs, outputs and technique.
Ground heights along a track are given as arr.
A ridge is a stretch of at least three heights that rises strictly for a while and then falls strictly for a while: there is a position inside it, neither end, where everything before rises strictly up to it and everything after falls strictly away from it.
Return the length of the longest ridge, or 0 when the track holds none.
Examples
Example 1
- Input
- arr = [3, 8, 14, 9, 4, 4, 11, 2]
- Output
- 5
The heights rise 3, 8, 14 and then fall 9, 4, which is a ridge of five. The fall stops at the level pair of fours, so the ridge cannot reach further.
Example 2
- Input
- arr = [1, 2, 3, 4, 5]
- Output
- 0
The track only rises, so there is no peak and no ridge.
Example 3
- Input
- arr = [1, 2, 2, 1]
- Output
- 0
The two level heights in the middle mean neither is a peak, since a peak needs its neighbours strictly lower.
Constraints
- 1 <= arr.length <= 10^4
- 0 <= arr[i] <= 10^4
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def longest_mountain(arr: list[int]) -> int:public int longestMountain(int[] arr)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 11 test cases: 11 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Maximum Frequency After Subarray OperationReal-world version: Most Dials Reading k After One NudgeMedium
- Longest String ChainReal-world version: Longest Chain of Grown LabelsMedium
- Trapping Rain WaterReal-world version: Level Flood CapacityHard
- Partition Array Into Two Arrays to Minimize Sum DifferenceReal-world version: Balanced Pontoon BallastHard