53. Maximum Subarray

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/t6_17/article/details/78544801

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.

给出一个长度为N的序列:求a1,a2,……,an中最大连续的序列和。
如数组[-2,1,-3,4,-1,2,1,-5,4],最大子序列和为 [4,-1,2,1],结果为6。

解题思路

这道题比较好的解法是经典的动态规划。利用状态转移函数:

sum(j) = max{sum(j)+a[j], a[j]}, //时间复杂度 O(n)

sum[i]记录以a[i]为子序列末端的最大连续和。在dp的过程中便可以更新sum数组的最大值以及两个边界。

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int result = INT_MIN;
        int sum = 0;
        for (int i = 0; i < nums.size(); i++) {
            sum = max(sum+nums[i], nums[i]);
            result = max(result, sum);
        }
        return result;
    }
};
展开阅读全文

Maximum Subarray Problem

07-18

Given an array A, we want to find the nonempty, contiguous subarray of A whose values have the largest sum. This problem is called the maximum subarray problem. For example, let A[16]=13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7, and the maximum subarray of A is 18,20,-7,12.rnrnThe pseudocode of the maximum subarray problem is as following:rn[img=https://img-bbs.csdn.net/upload/201307/18/1374150651_775626.png][/img]rn[img=https://img-bbs.csdn.net/upload/201307/18/1374150678_483036.png][/img]rnrn[b]请大家帮忙看看,以下程序有什么问题。为什么运行不了?欢迎大家指正。rnrn#include rn#include rnrnrnstruct tuplern int low;rn int high;rn int sum;rn;rnrnrnstruct tuple FindMaxCrossingSubarray (int a1[], int low, int mid, int high);rnstruct tuple FindMaxSubarray (int a2[], int low, int high);rnrnrn//找跨子列A[low..mid]和A[mid+1..high]的最大和子列rnrnstruct tuple FindMaxCrossingSubarray (int a1[], int low, int mid, int high)rnrn int leftsum=-1000, rightsum=-1000;rn int i, j, sum=0, maxleft, maxright;rn struct tuple p1;rnrn for (i=mid; i>=low; i--)rn rn sum=sum+a1[i];rn if (sum>leftsum)rn leftsum=sum;rn maxleft=i;rn rn rn //printf("%d, %d\n", maxleft, leftsum);rnrn rn sum=0;rn for (j=mid+1; j<=high; j++)rn rn sum=sum+a1[j];rn if (sum>rightsum)rn rightsum=sum;rn maxright=j;rn rn rn // printf("%d, %d\n", maxright, rightsum);rn //printf("%d, %d, %d\n",maxleft,maxright,leftsum+rightsum);rn //return (maxleft,maxright,leftsum+rightsum);rnrn p1.low=maxleft;rn p1.high=maxright;rn p1.sum=leftsum+rightsum;rn return p1;rnrnrnrnrnrn//找数列A[low..high]的最大和子列rnrnstruct tuple FindMaxSubarray (int a2[], int low, int high)rnrn int mid;rn int leftlow, lefthigh, leftsum, rightlow, righthigh, rightsum, crosslow, crosshigh, crosssum;rn struct tuple p1;rn struct tuple leftSubarray;rn struct tuple rightSubarray;rn struct tuple crossSubarray;rnrn if (high==low)rn rn p1.low=low;rn p1.high=low;rn p1.sum=a2[low];rn return p1; rn // return (low, high,a2[low]);rn rnrn else mid=(low+high)/2;rn rn leftSubarray=FindMaxSubarray (a2,low,high);rnrn rightSubarray=FindMaxSubarray (a2,mid+1,high);rn rn crossSubarray=FindMaxCrossingSubarray (a2, low, mid, high);rnrn if (leftSubarray.sum>=rightSubarray.sum && leftSubarray.sum>=crossSubarray.sum)rn return (leftSubarray);rn else if (rightSubarray.sum>=leftSubarray.sum && rightSubarray.sum>=crossSubarray.sum)rn return (rightSubarray);rn else return (crossSubarray);rnrnrnrnint main (void)rnrn int A[16]=13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7;rn struct tuple p1;rn p1= FindMaxSubarray (A, 0, 15);rn printf("The maximum subarray of A start at: %d, and end at: %d.\n", p1.low, p1.high);rn printf("The maximum is: %d\n", p1.sum);rn rnrn system ("pause");rn return 0;rn 论坛

没有更多推荐了,返回首页