Valid Mountain Array
Real-world version: Is the Survey Line a Single RidgeThe statement here is our own: the same inputs, outputs and technique.
A survey walks a straight line and records the ground elevation at each station, giving heights in walking order.
The line is a single ridge when both of the following hold:
- it has at least three stations;
- there is a station
p, neither the first nor the last, such that the elevations strictly increase from the first station up top, and strictly decrease frompon to the last station.
Strictly means no two neighbouring stations on either side may record the same elevation. Return true when the line is a single ridge and false otherwise.
Examples
Example 1
- Input
- heights = [0, 3, 1]
- Output
- true
Station 1 is neither the first nor the last, the elevation rises from 0 to 3 before it, and falls from 3 to 1 after it.
Example 2
- Input
- heights = [4, 7, 9, 11]
- Output
- false
The elevations keep rising to the last station, so there is no station strictly inside the line with a fall after it.
Example 3
- Input
- heights = [1, 2, 2, 1]
- Output
- false
Stations 1 and 2 both record 2. That pair is neither a strict rise nor a strict fall, so no station splits the line into the two required parts.
Constraints
- 1 <= heights.length <= 10^4
- 0 <= heights[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 is_single_ridge(heights: list[int]) -> bool:public boolean isSingleRidge(int[] heights)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 16 test cases: 16 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