Defines the concept of a local minimum. Arr has a length of 1 o'clock, arr[0] is the local smallest. If Arr has a length of N (n>1), if arr[0]<arr[1], then arr[0] is the local minimum; if arr[n-1]<arr[n-2], then arr[n-1] is the local smallest; if 0<i<n-1, Both arr[i]<arr[i-1] and arr[i]<arr[i+1], then Arr[i] is the local smallest.
Given an unordered array of arr, it is known that any two contiguous numbers in Arr are unequal, write a function, and simply return to any of the local smallest occurrences in arr.
Analysis:
If arr[0]<arr[1], then arr[0] is the local smallest;--return 0
If Arr[n-1]<arr[n-2], then arr[n-1] is the local smallest;--return 1
If arr[0] and arr[n-1] are not, then left = 1, right = n+2, Mid = (left+right)/2
If ARR[MID] < arr[mid+1] and arr[mid]<arr[mid-1], then return to mid
Otherwise there will be Arr[mid] < arr[mid+1] or arr[mid]<arr[mid-1], assuming Arr[mid] < arr[mid+1]
Because, arr[0]<arr[1], Arr[mid] < arr[mid+1], arr[1] to Arr[mid] than the existence of a local minimum, so repeated iteration. The time Complexity O (LGN) is better than the traversed O (n).
http://www.nowcoder.com/profile/864393/test/231563/24592
classsolution{ Public: intGetlessindex (vector<int>arr) { if(arr.size () = =0) return-1; if(arr.size () = =1) return 0; if(arr[0] < arr[1]) return 0; intSize =arr.size (); if(Arr[size-1] < arr[size-2]) returnSize-1; intLow =1; intHigh = size-2; intmid; while(Low <High ) {Mid= (low + high)/2; if(Arr[mid] > arr[mid+1]) { low= mid+1; } Else if(Arr[mid] > arr[mid-1]) { high= mid-1; } Else returnmid; } returnLow ; }};
[Nowcoder] Local minimum value position