(leetcode题解)Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

这是一道经典的题目,给定一个数组求和最大的子数组。算法导论对这道给出了两种解法,一种是分治策略,另一种是动态规划。

动态规划的解法是自底向上的方法,首先要找到题目的递推关系,这道题隐含了一个递推关系:pre=max(pre+nums[i],nums[i]),pre当前值下子数组和的最大值。

因而只要获取pre的最大值即可。C++实现如下。

int maxSubArray(vector<int>& nums) {
        int length=nums.size();
        int i=0;
        int max=nums[0];
        int pre=nums[0];
        i++;
        while(i<length)
        {
            if(pre+nums[i]>nums[i])
                pre=nums[i]+pre;
            else
                pre=nums[i];
            if(max<pre)
                max=pre;
            i++;
        }
        return max;
    }

分治策略是将问题的规模不断的缩小,但问题本质不变。

这道题采用二分法,最大子数组可以在下列三种情况获得:

1、最大值在[left,mid-1]

2、最大值在[mid+1,right]

3、最大值跨越mid,需要计算mid之前的最大值以及之后的最大值,然后三者之和就是最大值

C++实现如下:

int maxSubArray(vector<int>& nums) {
       return divide(nums,0,nums.size()-1,INT_MIN);
    }
    int divide(vector<int>& nums,int low,int high,int tmax)
    {
        if(low>high)
            return INT_MIN;
        int mid=(low+high)/2;
        int lmax=divide(nums,low,mid-1,tmax);
        int hmax=divide(nums,mid+1,high,tmax);
        tmax=max(lmax,tmax);
        tmax=max(hmax,tmax);
        int lnum=0;
        int ltemp=0;
        for(int i=mid-1;i>=low;i--)
        {
            lnum+=nums[i];
            if(lnum>ltemp)
                ltemp=lnum;
        }
        int hnum=0;
        int htemp=0;
        for(int i=mid+1;i<=high;i++)
        {
            hnum+=nums[i];
            if(hnum>htemp)
                htemp=hnum;
        }
        return max(tmax,(htemp+ltemp+nums[mid]));
    }
时间: 2024-10-27 02:05:35

(leetcode题解)Maximum Subarray的相关文章

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

Leetcode 动态规划 Maximum Subarray

正整数或一位小数或者俩位小数的正则表达式的写法 ^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$ Leetcode 动态规划 Maximum Subarray,布布扣,bubuko.com

LeetCode之Maximum Subarray

Maximum Subarray的通过率居然这么高,我提交了几次都是Wrong Answer,郁闷! Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has t

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod

LeetCode 53. Maximum Subarray(最大的子数组)

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. click to show more practice. Mor

41. leetcode 53. Maximum Subarray

53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6. 思路:这个题还挺经典

【leetcode】Maximum Subarray (53)

1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the contiguous subarray [4, -1, 2, 1] has the largest s

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. click to show

leetcode-Maximum Subarray

https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the l

19.2.8 [LeetCode 53] Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If