Nine Chapters count judges Net-original websitehttp://www.jiuzhang.com/problem/33/
Topics
An array of A[1..N], assuming that the array does not have any adjacent two numbers equal, satisfies the a[1]<a[2],a[n-1]>n. A[i] is called the crest, when and only if a[i]>a[i-1] and a[i]>a[i+1]. Please find a crest in the array. Suppose there are adjacent equal numbers in the array, what do we do?
Answer
According to the condition A[1]<a[2], a[n-1]>a[n] There must be A crest in A. Using the Dichotomy method, select mid-Midpoint, if A[MID] is the crest, then get the result; if a[mid]<a[mid-1] there must be a crest on the left; otherwise a[mid]<a[mid+1], there must be a crest on the right.
Interviewer Angle
This examines the dichotomy. From the perspective of thinking, first of all, the interviewer must analyze the meaning of the several conditions given in the topic (there must be a crest in a), and then gradually narrow the range of possible solutions by the dichotomy method. After answering the solution of the two-point method, it is further asked if the adjacent two numbers in a may be equal, what should be done? At this point, the interviewer needs to comb the condition from the beginning, analysis, a does not necessarily exist crest, each time after two minutes, it is difficult to determine which side will have a crest each time. Therefore, if the adjacent two numbers may be equal, the algorithm complexity of O (n) is required to find the crest in a.
Nine-chapter algorithm surface question 33 array crest