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>=VAL2) r=Mid2;Ten if(VAL1<=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