Nine-chapter algorithm surface question 33 array crest

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.