LeetCode刷题(C++)——Maximum Subarray(Easy)

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.

思路:
(1)暴力枚举法:起点i=0...n-1,终点j=i....n-1,然后求和i...j,时间复杂度为O(n^3)
(2)枚举(优化):起点i=0...n-1,终点j=i...n-1,同时求和i...j,时间复杂度为(O(n^2)
(3)分治法:
     分:将数组分成两个基本等长的子数组,分别求解T(n/2)
     合:跨越中心点的最大子数组(枚举),时间复杂度为O(n)
     总的时间复杂度为O(nlogn)

代码如下:

class Solution {
public:
    int maxSubArray(vector& nums) {
        int n = nums.size();
        if (n==0)
            return 0;
        if (n == 1)
            return nums[0];
        return maxSubArray(nums, 0, n - 1);
    }

    int maxSubArray(vector& nums, int start, int end)
    {
        if (start > end)
            return INT_MIN;
        int n = end-start+1;
        if (n == 1)
            return nums[start];
        int mid = (start+end)>>1;
        //分成两部分:0.....mid   |   mid+1......n-1
        int answer = max(maxSubArray(nums,start,mid-1), maxSubArray(nums,mid+1,end));
        int now = nums[mid], may = now;
        for (int i = mid - 1;i >= 0; i--)
        {
            may = max(may, now += nums[i]);
        }
        now = may;
        for (int i = mid+1;i <= end;i++)
        {
            may = max(may, now +=nums[i]);
        }
        return max(answer, may);
    }
};

(4)利用动态规划思想
     假设dp[i]表示以nums[i]结尾的最大子数组的和
     dp[i]=max(dp[i-1]+nums[i],nums[i])
         最大子数组包含nums[i-1]:dp[i-1]+nums[i]
         最大子数组不包含nums[i-1]:nums[i]
         两者当中取最大值
     初值dp[0]=nums[0]
     最大子数组的和为dp[0]...dp[n-1]中最大者
     时间复杂度为O(n),空间复杂度为O(n)
代码如下:
class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        vector dp(n);
        dp[0]=nums[0];
        int answer = dp[0];
        for(int i=1; i
同时还可以对dp进行空间优化:使得空间复杂度为O(1),用一个变量endHere来存储目前最大的子数组和
     endHere = max(endHere+nums[i],nums[i])          //  等号左边的endHere相当于dp[i],等号右边的相当于dp[i-1]
     answer = max(endHere,answer)
代码如下:
class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        int endHere = nums[0];
        int answer = nums[0];
        for(int i=1; i
(5)线性枚举
     定义sum[i]=nums[0]+nums[1]+....+nums[i];  i>=0
     则对于i...j的和:nums[i]+nums[i+1]+...+nums[j]=sum[j]-sum[i-1]
     对于当前的sum[j],当sum[i-1]取最小值时,sum[j]才是最大值,于是我们可以用一个变量来记录sum的最小值
     时间复杂度为O(n),空间复杂度为O(1)
代码如下:
class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        int sum = nums[0];
        int minSum = min(sum,0);
        int answer = nums[0];
        for(int i=1; i


(6) 从头到尾,只需要遍历一遍数组,时间复杂度为O(n),空间复杂度为O(1)

设置一个变量subSum记录nums[i]之前子数组和,最大子数组记为sum

当subSum<=0时,subSum+nums[i]<=nums[i],此时令subSum=nums[i],

当subSum>0时,subSum+nums[i]>nums[i]

比较:sum = max(sum,subSum)

最后返回sum

代码如下:

class Solution {
public:
    int maxSubArray(vector& nums) {
        int n=nums.size();
        if(n==0)
            return 0;
        int subSum = 0;
        int sum = INT_MIN;
        for(int i=0; i






你可能感兴趣的:(LeetCode刷题(C++))