Algorithm-oldest sequence and C + + implementation (three levels of complexity)

Source: Internet
Author: User

The oldest sequence and problems are very easy:

is an array, which is the maximum value of the continuous segment and the whole sequence. To find out the value.


First, the most complex: O (n3)

Directly on the code, very easy:

  main.cpp//  sumsequence////  Created by Alps on 14-7-23.//  Copyright (c) 2014 Chen. All rights reserved.//#include <iostream>using namespace std;int maxsubsequencesum (const int a[], int N) {    int T Hissum, Maxsum, I, J, K;    maxsum = 0;    for (i = 0, i < n; i++) {for        (j = i; J < N; J + +) {            thissum = 0;            for (k = i; k < J; k++) {                thissum + = A[k];            }            Maxsum = thissum > maxsum? thissum:maxsum;        }    }    return maxsum;} int main (int argc, const char * argv[]) {    int a[] = {1, 2, -5, 2, 5, 1, 8,-4};    int N = sizeof (A)/sizeof (int);//    printf ("%d\n", N);    int maxsum = Maxsubsequencesum (A, N);    printf ("%d\n", maxsum);    return 0;}

This fact is very easy, the first layer for loop is I start from the beginning to traverse. The second layer for IS J traversal from I to the tail. The third layer is the one that counts I to J.

The complexity of Time is O (N3).

Here's an O (N2):

The code is as follows:

  main.cpp//  sumsequencen2////  Created by Alps on 14-7-23.//  Copyright (c) 2014 Chen. All rights reserved.//#include <iostream>using namespace std;int maxsubsequencesum (const int a[], int N) {    int M Axsum, Thissum, I, J;    maxsum = 0;    for (i = 0; i < N; i++) {        thissum = 0;        for (j = i; J < N; J + +) {            thissum + = a[j];            Maxsum = maxsum > thissum? maxsum:thissum;        }    }    return maxsum;} int main (int argc, const char * argv[]) {    int a[] = {1, 2, -5, 2, 5, 1, 8,-4};    int N = sizeof (A)/sizeof (int);    printf ("%d\n", n);    int maxsum = Maxsubsequencesum (A, N);    printf ("%d\n", maxsum);        return 0;}
This is also better understood that the first layer of the loop is I from start to finish, the second loop is J from I traverse to the tail, in the process of continuous detection of thissum size, take Max (Thissum, maxsum) number, and assigned to Maxsum, so you can know how much maxsum ~


Another method of complexity is O (NLOGN) but this algorithm is more troublesome, the code is more troublesome, I do not write ~ Want to learn can go to the "Data structure and algorithm analysis" to learn.


Here is an O (n) level algorithm to solve the problem!!! : Please look at the code:


  main.cpp//  sumsequencen////  Created by Alps on 14-7-23.//  Copyright (c) 2014 Chen. All rights reserved.//#include <iostream>using namespace std;int maxsubsequencesum (const int a[], int N) {    int M Axsum, Thissum, I;    Maxsum = a[0];    thissum = 0;    for (i = 0; i < N; i++) {        thissum + = A[i];        Maxsum = thissum > maxsum? Thissum:maxsum;        if (Thissum < 0) {            thissum = 0;            Continue;        }    }    return maxsum;} int main (int argc, const char * argv[]) {    int a[] = {1, 2, -5, 2, 5, 1, 8,-4};    int N = sizeof (A)/sizeof (int);    printf ("%d\n", n);    int maxsum = Maxsubsequencesum (A, N);    printf ("%d\n", maxsum);    return 0;}

The O (n) level algorithm is the perfect algorithm. My understanding of this algorithm is that in an array, there are very many very many paragraphs, these segments have a sum, the smallest segment is an element, and the largest sequence and certainly is a paragraph, or two segments of the sum, and is added a positive number will become larger, so when a paragraph is negative, I just throw away ~ (unless all are negative, find the largest one.) )

So there's the algorithm above. Do not understand, please leave a message ~


Algorithm-oldest sequence and C + + implementation (three levels of complexity)

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.