First and Last Target Position in a Mountain Array
Problem statement
You are given an integer array mountain that strictly increases to one peak and then strictly decreases, plus an integer target. The peak is not an endpoint.
Because the two slopes may contain the same value, target can appear once on each side of the peak. Return [first, last], the first and last index where target occurs. If the target is absent, return [-1, -1].
Your algorithm must run in O(log n) time.
Function
searchMountainRange(mountain: int[], target: int) → int[]Examples
Example 1
mountain = [1,3,5,7,5,3,1]target = 3return = [1,5]The target appears at index 1 on the increasing slope and index 5 on the decreasing slope.
Example 2
mountain = [-5,-2,4,9,6,1]target = 9return = [3,3]The target is the peak, so its first and last positions are both 3.
Example 3
mountain = [0,2,8,6,4]target = 5return = [-1,-1]The target does not appear on either slope.
Constraints
3 <= mountain.length <= 100000.-10^9 <= mountain[i], target <= 10^9.- There is exactly one non-endpoint peak.
- The left slope is strictly increasing and the right slope is strictly decreasing.