The maximum continuous field problems and

The largest sub-segment and description of the problem

Is given by n integers (may be negative integers) consisting of a sequence a1, a2, a3 ... an, find the number of columns and the maximum consecutive sub-segments!

    For example: When (a1, a2, a3, a4, a5) = (- 2,11, -4,13, -5, -2), the maximum is 20 and the field (11 + (-4) + 13);

       The following examples are to int data [6] = {-2,11, -4,13, -5, -2}; int n = 6;

 Initialize the array: 

    // initialize the array 
    private static Integer [] array = { -2, 11, -4, 13, -5, -2};

An algorithm: for all satisfies 0 <= i <= j <= n of the (i, j) iterating integers, each integer pair, the program is calculated for array [i ... j] is the sum, and test if the sum is greater than the sum of the maximum date

Code concise, easy to understand, but the program execution speed is slow, the time complexity is O (n ^ 3)

  

  / ** 
     * time complexity of O (n-^. 3) 
     * / 
    public static maxSum1 Integer () { 
        int maxSum = 0; // store a maximum sub-segment and 
        int tempSum; // temporarily storing the maximum sub-segment and 
        for (int i 0 =; I <be array.length -. 1; I ++) { 
            for (int I = J; J <be array.length; J ++) { 
                tempSum = 0; 
                for (int I = K; K <= J; K ++) { 
                    tempSum Array = + [K]; 
                    IF (tempSum> maxSum) { 
                        maxSum = tempSum; 
                    } 
                } 
            } 
        } 
        return maxSum; 
    }

  

Two algorithms: an algorithm for a significant disadvantage, a large number of repeated calculations. We can note:

Code concise, easy to understand, as compared to a speed of the algorithm program to be executed faster, the time complexity is O (n ^ 2)

  Note: array [i ... j] to the sum of the previously calculated sum (array [i ... j-1]) is closely related to accumulate only on the basis thereof, without a lot of repeated calculations!

    / ** 
     * time complexity of O (n-^ 2) 
     * / 
    public static maxSum2 Integer () { 
        int maxSum = 0; // store a maximum sub-segment and 
        int tempSum; // temporarily storing the maximum sub-segment and 
        for (int i 0 =; I <be array.length -. 1; I ++) { 
            tempSum = 0; 
            for (int I = J; J <be array.length; J ++) { 
                tempSum + = Array [J]; 
                IF (tempSum> maxSum) { 
                    maxSum tempSum =; 
                } 
            } 
        } 
        return maxSum; 
    }

  

Three algorithms: Divide and Conquer algorithms can be used to solve using dichotomy half, and recursively solving each sub-segment is obtained continuously and the maximum left, right side and maximum consecutive sub-segments, as well as left and right sub-segments and maximum continuous sum, the three compare them to select a maximum value return! (I.e., the maximum value of this current is divided between the cells)

  Note: The code not easy understanding, but the program with respect to the two fast algorithm execution, the time complexity is O (n * logn)

  

  / ** 
     * Divide and Conquer Algorithm uses 
     * time complexity of O (n-* logN) 
     * / 
    public static Integer maxSum3 (int left, int right) { 
        int maxSum = 0; 
        end if (left == right) {// recursive conditions 
            IF (Array [left]> 0) { 
                maxSum = Array [left]; 
            } the else { 
                maxSum = 0; 
            } 
            return maxSum; 
        } 

        int MID = (left + right) / 2; 
        int leftMaxSum = maxSum3 (left, MID) ; // recursive solution left portion of the maximum 
        int rightMaxSum = maxSum3 (mid + 1 , right); // recursive solution right portions maximum 

        // solving the left and right of the maximum value and the maximum value  
        int leftMax = 0; // record the left maximum
        int leftMaxTemp = 0; // record the left the maximum value of the temporary variable
        for (int I = MID; I> = left; i--) { 
            leftMaxTemp + = Array [I]; 
            IF (leftMaxTemp> leftMax) { 
                leftMax = leftMaxTemp; // maximum value on the left leftMax 
            } 
        } 
        int = 0 rightMax ; 
        int rightMaxTemp = 0; 
        for (int. 1 + J = MID; J <= right; J ++) { 
            rightMaxTemp + = Array [J]; 
            IF (rightMaxTemp> rightMax) { 
                rightMax = rightMaxTemp; // maximum value on the right rightMax 
            } 
        }  
        maxSum + = leftMax rightMax; // (left and right maximum value and maximum value) calculating the maximum of the third sub-segment of the case and
        // comparator (maximum left) and (right maximum) and (maximum value and on both sides) are compared, select a maximum return 
        IF (maxSum <leftMaxSum) { 
            maxSum = leftMaxSum; 
        }
        if (<rightMaxSum) { 
            = rightMaxSum; 
        } 
        Return; 
    }

  

Algorithm 4: Use for solving dynamic programming, the Data [] array we easy to know, when maxSumTemp> time 0, maxSumTemp = data [i] + maxSumTemp (increasingly larger), or maxSumTemp = data [i] (otherwise increasingly The smaller)

  Code to facilitate the understanding, but the program executed relative to the speed of the fastest three algorithms, time complexity is O (n)

  

    / ** 
     * time complexity of O (n-) 
     * / 
    public static maxSum4 Integer () { 
        int = maxSum Array [0]; 
        int = maxSumTemp Array [0]; // initialization 

        for (int i = 1; i <array .length; I ++) { 
            IF (maxSumTemp> 0) {// maximum value of the temporary variable is greater than zero only, it may be increasingly larger 
                maxSumTemp + = Array [I]; 
            } maximum value of the else {// temporary variables only less than zero, direct equal data [i], or increasingly smaller 
                maxSumTemp = Array [I]; 
            } 
            IF (maxSumTemp> maxSum) {// determines assignment 
                maxSum = maxSumTemp; 
            } 
        } 
        return maxSum; 
    }

  

Test code:

public static void main (String [] args) { 
// Integer = maxSum1 (); 
// Integer = maxSum2 (); 
// Integer = maxSum3 (0 array.length - 1); 
        Integer = maxSum4 (); 
        System.out.println ( "=" +); 
    }

  A little progress every day, every month from the diary becomes a big cow!

Guess you like

Origin www.cnblogs.com/blogtech/p/11116524.html