Data structure and algorithm analysis (1)

Source: Internet
Author: User
Tags greatest common divisor pow

1. Maximum sub-sequence and problem
Input Sample: 4-3 5-2-1 2 6-2
Output: 11
1.1 Using the binary recursive solution code is as follows (time complexity: O (NLOGN)): GCC compiles C + + using g++ command

intGETMAX3 (intAintBintc) {    intMax =A; if(Max <b) Max=b; if(Max <c) Max=C; returnMax;}intGetmaxsum (Constvector<int> &arr,intBGNintend) {    if(BGN >=end)return 0; intMID = (BGN + END)/2; intLeftmaxsum =Getmaxsum (arr, BGN, Mid); //The starting position here should be set to mid + 1, otherwise it will cause infinite recursion    intRightmaxsum = Getmaxsum (arr, Mid +1, end); intLeftmaxborder =0, lefttmp =0;  for(inti = mid; I >= BGN; --i) {lefttmp+=Arr[i]; if(Lefttmp >leftmaxborder) Leftmaxborder=lefttmp; //if (lefttmp < 0)//This place cannot be exited prematurely and must be fully traversed//Break ;    }    intRightmaxborder =0, righttmp =0;  for(inti = mid +1; I < end; ++i) {righttmp+=Arr[i]; if(Righttmp >rightmaxborder) Rightmaxborder=righttmp; //if (righttmp < 0)//Break ;    }    returnGetMax3 (Leftmaxsum, rightmaxsum, Leftmaxborder +rightmaxborder);}

1.2 The solution for one-time traversal is as follows (Times complexity O (N)):

intGetmaxsubsum (Constvector<int> &arr,intBGNintend) {    intMaxsum =0; intSumtmp =0;  for(inti = BGN; I < end; ++i) {sumtmp+=Arr[i]; if(Sumtmp >maxsum) Maxsum=sumtmp; Else if(Sum <0) sumtmp=0; }    returnmaxsum;}

2. The algorithm time complexity is O (logn) Typical problem:
2.1 Pair lookup (binary search): Time complexity (<= log2n)

intBinsearch (Constvector<int> &arr,intBGNintEndinttarget)//end-tail element after a position {intRET =-1;  while(BGN <end) {        intMID = (BGN + END)/2; if(target = =Arr[mid]) {ret=mid;  Break; }        Else if(Target >Arr[mid]) BGN= Mid +1; ElseEnd=mid; }    returnret;}

2.2 Two integer greatest common divisor solution (Euclidean algorithm): Time complexity (<= 2logN)

int int int N) {    while0)    {        int rem = m% N;         = N;         = rem;    }     return m;}

Here is the idea of recursion: the largest common factor of M and N is the largest common factor of N and REM (m%n) ..., then, only the maximum common factor of N and REM is the maximum common factor of M and N to allow this recursion to go on. So the question is, why is the largest common factor of N and REM the largest common factor of M and n?

Idea: Suppose m > N, and even if M < N, after a traversal has M > n,m and n the largest common factor is a, then M = XA, n = YA.
rem = m%n-M-zn (z = m/n), when Rem=0,n is the largest common factor of both; Rem>0,rem=xa-zya, obviously rem%a = 0
The largest common factor of M and N is the largest common factor of N and REM, and so on

2.3 Power Operation: Time complexity (<= 2logN)

 long  long  Pow ( long  long  x, unsigned int   N) { if  (0  = =  N)  return  1     ;  if  (n% 2  )  return  pow (x*x, N/2 ) * x;  else  return  pow (x*x, N/2   

With 2^15 as input, you need 2log (15) = 6 multiplication
With 2^16 as input, you only need 4+1 (<2log (16) =8) multiplication, or you can modify the code to add an exit judgment (if (1 = = N) to make it 4 times multiplication, but you need to enter the POW to judge once more

Ps:1. The book takes the 2^62 for example, altogether needs 9 multiplication operation, but the algorithm process which the deduction individual thought has the question. The actual process should be the first layer to calculate the x*x value of the POW inlet parameter until n = = 0.
Then, reverse-order the values of the pow*x, until the algorithm ends the operation
2. The Code Statement POW (x*x, N/2) can be replaced with POW (x, N/2) *pow (x, N/2), but the efficiency is very low, because it does a lot of repetitive work

Data structure and algorithm analysis (1)

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.