1052. Mountain Summit Probe
A surveyor records the elevation along a trail that climbs steadily to a single summit and then descends steadily, with no flat stretches. The readings are hidden behind a MountainArray called mountain: mountain.get(i) returns the elevation at position i, and mountain.length() returns the number of positions.
Return the index of the summit, which is the only position higher than both of its neighbours. The trail has at least 3 positions, and the readings strictly increase up to the summit and strictly decrease after it. Each call to get or length counts, and you may make at most 40 calls in total, so you must not scan the trail.
Example 1
- Input:
- mountain = [1,4,9,6,2]
- Output:
- 2
- Explanation:
The readings rise to 9 at index 2 and fall afterwards, so the summit is at index 2.
Example 2
- Input:
- mountain = [0,3,1]
- Output:
- 1
- Explanation:
The shortest valid trail peaks in the middle at index 1.
Constraints
3 ≤ length ≤ 2000
0 ≤ elevation ≤ 109
Strictly increasing then strictly decreasing
At most 40 calls in total
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Calls
- You may call
MountainArray.get(index), MountainArray.length()at most 40 times per case. It is provided; you do not write it.
Expected complexity
- Time
- O(log n) calls
- Space
- O(1)