LeetCode--Python解析【Maximum Subarray】(53)

题目:


方法:

这道题虽然是easy tag,但是从编程思想上来说还是有一定难度,参考了别人的算法进行完成。

算法时间复杂度为O(n),一次遍历数组,对数组进行累加的操作。

需要维护两个变量,分别为局部最优curr_sum,和全局最优max_sum。

遍历数组时,从第一个元素开始累加,并赋值给局部最优curr_sum,当局部最优为负数时,可放弃对应子串,重置局部最优为0。

每一次计算出新的局部最优时,与当前全局最优比较,将较大的值付给全局最优。

最后通过一次遍历,找到子串的最大和。

返回,结束。

class Solution:
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        curr_sum = 0
        max_sum = -100000000000
        for n in nums:
            if curr_sum < 0:
                curr_sum = 0
            curr_sum += n
            max_sum = max(max_sum , curr_sum)
        return max_sum

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=http://img.bbs.csdn.net/upload/201307/18/1374150651_775626.png][/img]rn[img=http://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

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试