Wins the Offer-30. The maximum and successive subarray (C ++ / Java)

topic:

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)

analysis:

In fact, seeking the largest contiguous sequence and.

We maintain a temporary sequence and, through the array to calculate temporary sequence and, if temporary sequence and greater than the maximum, the maximum value is updated, when the temporary sequence and less than zero, there is no proof at this time sequence of room for growth, and will and updating temporary sequence is 0, the sequence and the subsequent recalculation, and returns to the maximum value.

You can also use divide and conquer to ask this question. For an array, the most dramatic and continuous sequence, or to the left of the middle element, or to the right of the middle element, or across the middle element, in accordance with this partition may be utilized to seek. The maximum sequence across the intermediate element and its result that the intermediate element is calculated from both sides, we can start with the intermediate element, respectively, and solving the largest sequence, two sequences and finally returns to the ends and, note that the intermediate element is only counted once.

program:

C++

class Solution {
public:
    int FindGreatestSumOfSubArray(vector<int> array) {
        if(array.size() == 0)
            return 0;
        return helper(array, 0, array.size()-1);
    }
    int helper(vector<int>& nums, int left, int right){
        if(left == right)
            return nums[left];
        int mid = (left + right) / 2;
        int maxResult = max(helper(nums, left, mid), helper(nums, mid+1, right));
        maxResult = max(maxResult, maxCrossMid(nums, mid, left, right));
        return maxResult;
    }
    int maxCrossMid(vector<int>& nums, int mid, int left, int right){
        int lsum = INT_MIN;
        int tempSum = 0;
        for(int i = mid; i >= left; --i){
            tempSum += nums[i];
            lsum = max(tempSum, lsum);
        }
        tempSum = 0;
        int rsum = INT_MIN;
        for(int i = mid+1; i <= right; ++i){
            tempSum += nums[i];
            rsum = max(tempSum, rsum);
        }
        return (lsum + rsum);
    }
};

Java

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array.length == 0)
            return 0;
        int max = array[0];
        int tempSum = 0;
        for(int num:array){
            tempSum += num;
            if(max < tempSum)
                max = tempSum;
            if(tempSum < 0)
                tempSum = 0;
        }
        return max;
    }
}

 

Guess you like

Origin www.cnblogs.com/silentteller/p/11960237.html