"Algorithm Learning" method of three points

Source: Internet
Author: User

We all know the binary search and the application of many binary points.

However, the two points are mainly used for functions or sequences with monotonicity.

If the function/sequence is not monotonic, it has a single-peak/Valley feature.

We can use the three-point method to determine the extremum of this function.

The specific ideas of the three-part method can be seen elsewhere.

I put a template of my own, no bug ...

Because I used to be a bug template entrap ...

The speed is very fast and the constants should be very small. About twice times the constant of two points.

1L=l,r=r;//determine the initial value of l,r, ensure l<=r oh2  while(l!=R) {3     if(r-l<3){//[L,r] more than 3 on the special award4Ans=max4 (Ans,f (i,l), F (i,l+1), F (i,r));5          Break;6     }7mid1= (L+R)/2, mid2=mid1+1;//close the two point to the midpoint and decrease the constant8Val1=f (I,MID1), val2=f (I,MID2);9     if(VAL1&GT;=VAL2) r=Mid2;Ten     if(VAL1&LT;=VAL2) l=Mid1; One } A if(L==R) Ans=max (Ans,f (i,l));//for the l==r at the beginning of the contract

Algorithm learning three-part method

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.