And the maximum value of the sub-arrays

Problem Description

Given a sequence \ (A_0 \) , \ (A_1 \) , \ (A_2 \) , ..., \ (. 1-n-A_ {} \) , seeking \ (A_i + A_ {i + 1} +. .. + A_j \) maximum.

A solution

Violence enumeration left endpoint \ (I \) and right points \ (J \) , after the calculation of \ (A_i \) and \ (A_j \) between and, time complexity \ (O (n ^ 3) \) it is easy to TLE.

#define INF 0x7FFFFFFF

int sub_sum(int a[],int n)
{
    int MAX = -INF;
    for(int i = 0;i < n;i++)
    {
        for (int j = i; j < n; j++)
         {
            int temp = 0;
            for (int k = i; k <= j; k++)
            {
                    temp += a[k];
            }
            if (temp > MAX)
            {
                MAX = temp;
            }
         }
    }
    return MAX;
}

Solution two

Prefix recording and pre-processing the input data \ (SUM [I] = A [0] + ... + A [I] \) , so \ (A_i + A_ {i + 1} + ... + A_j = SUM [J] -sum [-I. 1] \) , for the complexity of optimization \ (O (^ n-2) \) .

int sub_sum(int a[],int n)
{
    int MAX = -INF;
    for(int i = 0;i < n;i++)
    {
        for(int j = i;j < n;j++}
        {
            int temp = sum[j] - sum[i - 1];
            if(temp > MAX)
                MAX = temp;
            else
                temp = 0;
        }
    }
    return MAX;
}

Understanding Three

Dynamic programming, complexity \ (O (the n-) \) .
Defines the state array \ (DP [I] \) , represented by \ (A [i] \) and maximum, so that only two end continuous sequences:
1, only the contiguous sequence \ (A [i] \ ) this element;
2, the sequence of a plurality of elements from the previous \ (a [p] \) begins to \ (a [i] \) ends.
For one, the maximum and that \ (A [I] \) ;
for 2, maximum and is \ (DP [I - 1] + A [I] \) , since \ (dp [i] \) required to \ ( A [i] \) at the end, even if the \ (A [i] \) is negative, \ (DP [I] \) is still equal to \ (DP [I -. 1] + A [i] \) .
Therefore, the state transition equation is:
\ [DP [I] = max {\ {A [I], DP [I-. 1] + A [I] \}} \] boundaries are \ (dp [0] = A [0 ] \) .
So enumerate \ (i \)To give \ (DP \) array, obtains \ (DP \) array to a maximum value.

Can be seen, each computing \ (dp [i] \) used only \ (DP [-I. 1] \) , do not directly use the information before, this is the state of no aftereffect , the only way, the dynamic They may be planning to get the right result.

int dp[5010];
dp[0] = a[0];

int sub_sum(int a[],int n)
{
    for(int i = 1;i < n;i++)
    {
        //状态转移方程
        dp[i] = max(a[i],dp[i - 1] + a[i]);
    }

    int k = 0;
    for(int i = 1;i < n;i++)
    {
        if(dp[i] > dp[k])
            k = i;
    }
    return dp[k];
}

To avoid \ (DP [] \) array may be optimized for space complexity \ (O (. 1) \) :

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int allSum = INT_MIN, curSum = 0;
        
        int n = nums.size();
        for(int i = 0;i < n;i++)
        {
            curSum = max(nums[i], curSum + nums[i]);
            if(curSum > allSum)
            {
                allSum = curSum;
            }
        }
        
        return allSum;
    }
};

Guess you like

Origin www.cnblogs.com/EIMadrigal/p/12130801.html