[Offer] to prove safety and maximum (rookie and the power of God and the idea of code) consecutive sub-array

Topic restated:

HZ occasionally get some professional issues to flicker those non-computer science students. After the test group will finish today, he spoke up: in the old one-dimensional pattern recognition, it is often necessary to calculate the maximum and continuous sub-vector when the vector is a positive whole number, the problem solved. However, if the vector contains a negative number, it should contain a negative number, and expect a positive number next to it would make up for it? For example: {6, -3, -2,7, -15,1,2,2}, and the maximum successive sub-vectors of 8 (beginning from 0, up to the third). To an array and returns its maximum continuous subsequence and you will not be fooled him live? (Sub-vector length is at least 1)
to make it easy. But people do compare, I do question it?

First Writing and rookie (rookie is my own)

Rookie ideas:

1- maximum and be initialized, and the maximum number is the largest in the array.
2- The array is then continuously arranged in combination, are obtained containing 2, 3, ... n subarray elements and accumulated in the maximum value.
(Ideas are nothing to say, the idea is to enumerate than the size of)
Here Insert Picture Description
the rookie thinking Code:


```java
class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        int len = array.size();
        int result=array[0] ;
        int k = 1;
        if (len == 1) {
            return result;
        }
        else {
            for (int j = 0; j < len; j++) {
                if (array[j] > result)
                    result = array[j];
            }
            while (k <= len) {
                result = maxAdd(array, k,result);
                k++;
            }

        }
        return result;
    }

    int maxAdd(vector<int>array, int k,int res) {
        int l = array.size();
       // int res =0;
        //if(k=1)
        for (int i = 0; i <= l - k; ++i) {
            int tmp = 0;
            for (int j = 0; j < k; ++j) {
                tmp += array[i+j];
            }
            if (tmp > res)
                res = tmp;
        }
        return res;
    }
};

Great God idea:

Serious look at the idea of the great God, which is dynamic programming. As a rookie I really admire, regressed. I feel unlimited garbage.
Link: https: //www.nowcoder.com/questionTerminal/459bd355da1549fa8a49e350bf3df484 f = discussion?
Source: Cattle-off network

Recording the cumulative value of the flag, result recording and the largest
based on the idea: For a number A, if left A cumulative number of non-negative, then add A can make not less than A, considered the cumulative value of the whole and are contributing. If the first several negative accumulated value, it is considered harmful to the sum, flag to record the current value.
At this time, if the flag is used and the result is greater than the result recorded

Rookie recovery code

int FindGreatestSumOfSubArray(vector<int> array) {
    int len = array.size();
    int result = array[0];
    if (len == 1)
        return result;
    else {
        int flag = array[0];
        for (int i = 1; i < len; ++i) {
            if (flag >= 0)
                flag += array[i];
            else
                flag = array[i];
            if (flag > result)
                result = flag;
        }
        return result;

    }
}

Published 57 original articles · won praise 28 · views 4118

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/104618247