首页 > 其他 > 详细

leetcode 53-Maximum Subarray(medium)

时间:2018-09-23 14:52:14      阅读:135      评论:0      收藏:0      [点我收藏+]

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

 

1. use an array to store the sum of 0->i num in nums;(the difference between two sum in this array is the subarray sum)

2. finding the maximum array is to find the maximum of the difference(a latter sum minus a former sum)

class Solution {
    public int maxSubArray(int[] nums) {
        if(nums.length==0) return 0;
        int sum=0;
        int[] record=new int[nums.length];
        for(int i=0;i<nums.length;i++){
            sum+=nums[i];
            record[i]=sum;
        }
        int max=Integer.MIN_VALUE;
        int min=0;
        for(int i=0;i<nums.length;i++){
            if(record[i]-min>max) max=record[i]-min;
            if(record[i]<min) min=record[i];
        }
        return max;
    }
}

 

 

 

2. DP: https://leetcode.com/problems/maximum-subarray/discuss/20193/DP-solution-and-some-thoughts

Analysis of this problem:
Apparently, this is a optimization problem, which can be usually solved by DP. So when it comes to DP, the first thing for us to figure out is the format of the sub problem(or the state of each sub problem). The format of the sub problem can be helpful when we are trying to come up with the recursive relation.

 

At first, I think the sub problem should look like: maxSubArray(int A[], int i, int j), which means the maxSubArray for A[i: j]. In this way, our goal is to figure out what maxSubArray(A, 0, A.length - 1) is. However, if we define the format of the sub problem in this way, it‘s hard to find the connection from the sub problem to the original problem(at least for me). In other words, I can‘t find a way to divided the original problem into the sub problems and use the solutions of the sub problems to somehow create the solution of the original one.

 

So I change the format of the sub problem into something like: maxSubArray(int A[], int i), which means the maxSubArray for A[0:i ] which must has A[i] as the end element. Note that now the sub problem‘s format is less flexible and less powerful than the previous one because there‘s a limitation that A[i] should be contained in that sequence and we have to keep track of each solution of the sub problem to update the global optimal value. However, now the connect between the sub problem & the original one becomes clearer:

maxSubArray(A, i) = maxSubArray(A, i - 1) > 0 ? maxSubArray(A, i - 1) : 0 + A[i]; 

 

class Solution {
    public int maxSubArray(int[] nums) {
        int[] dp=new int[nums.length];
        dp[0]=nums[0];
        int max=nums[0];
        for(int i=1;i<nums.length;i++){
            dp[i]=nums[i]+(dp[i-1]>0?dp[i-1]:0);
            if(max<dp[i]) max=dp[i];
        }
        return max;
    }
}

 

in fact no need for the dp array, since we only need to store the max and the biggest sum array ending at i-1 position

class Solution {
    public int maxSubArray(int[] nums) {
        int max=nums[0];
        int pre=nums[0];
        for(int i=1;i<nums.length;i++){
            pre=nums[i]+(pre>0?pre:0);
            max=Math.max(max, pre);
        }
        return max;
    }
}

 

leetcode 53-Maximum Subarray(medium)

原文:https://www.cnblogs.com/yshi12/p/9692571.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!