Find the contiguous subarray within an array (containing at least one number) which have the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4]
,
The contiguous Subarray has the [4,-1,2,1]
largest sum = 6
.
Click to show more practice.
More Practice:
If you had figured out the O (n) solution, try coding another solution using the divide and conquer approach, WHI CH is more subtle.
Title tag: Array This topic gives us an array that lets us find a contiguous subarray whose sum is the largest. The topic description has the O (n) method and the Divide and conquer method. Let's take a look at the O (n) method: Iterate over the array, for each number, we judge (the sum + this number before) and (this number) than the size, if (this number) itself is larger than (the sum of the previous), then the description does not need to continue to add, directly from this number And began to go on, because it was already bigger than the sum of the previous sums. Conversely, if (the previous sum + this number) is greater than (this number) continue to add. This method is similar to Kadane algorithm, Kadane algorithm is, if the previous sum is less than 0, recalculate sum, if sum is not less than 0, then continue to add. Then look at the divide and conquer method: There are three possibilities for any array: 1. Its maximum subarray falls on its left side; 2. Maximum Subarray falls on its right side; 3. Maximum Subarray falls in the middle of it. For the first, two cases, the use of dichotomy is easy to get, base case is if there is only one number, then return. For the third case, if you fall in the middle, then we have to return from the left and right side of the two MSS, pick out a large, and then from (the big value) and (left + to) in the pick out a large. See the code below for details.
Java Solution 1:
Runtime beats 71.37%
Completion Date: 03/28/2017
Keywords: Array
Key points: Change based on Kadane ' s algorithm
1 Public classSolution2 {3 Public intMaxsubarray (int[] nums)4 {5 //solution 1:o (n)6 //check param validation.7 if(Nums = =NULL|| Nums.length = = 0)8 return0;9 Ten intsum = 0; One intMax =Integer.min_value; A - //iterate nums array. - for(inti = 0; i < nums.length; i++) the { - //Choose a larger one between current number or (previous sum + current number). -sum = Math.max (Nums[i], sum +nums[i]); -max = Math.max (max, sum);//choose the larger max. + } - + returnMax; A } at - - -}
Java Solution 2:
Runtime beats 71.37%
Completion Date: 03/28/2017
Keywords: Array
Key points: Kadane ' s algorithm
1 Public classSolution2 {3 Public intMaxsubarray (int[] nums)4 {5 intMax_ending_here = 0;6 intMax_so_far =Integer.min_value;7 8 for(inti = 0; i < nums.length; i++)9 { Ten if(Max_ending_here < 0) OneMax_ending_here = 0; AMax_ending_here + =Nums[i]; -Max_so_far =Math.max (Max_so_far, Max_ending_here); - } the returnMax_so_far; - } - - + -}
Java Solution 3:
Runtime beats 29.96%
Completion Date: 03/29/2017
Keywords: Array
Key points: Divide and conquer
1 Public classSolution2 {3 Public intMaxsubarray (int[] nums)4 {5 //solution 3:divide and conquer. O (NLOGN)6 if(Nums = =NULL|| Nums.length = = 0)7 return0;8 9 Ten returnMax_subarray_sum (nums, 0, Nums.length-1); One } A - Public intMax_subarray_sum (int[] Nums,intLeftintRight ) - { the if(left = right)//base Case:meaning There is only one element. - returnNums[left]; - - intMiddle = (left + right)/2;//calculate the middle one. + - //recursively call Max_subarray_sum to go down to the base case. + intLEFT_MSS =max_subarray_sum (Nums, left, middle); A intRIGHT_MSS = Max_subarray_sum (Nums, middle+1, right); at - //set up leftsum, Rightsum and sum. - intLeftsum =Integer.min_value; - intRightsum =Integer.min_value; - intsum = 0; - in //calculate the maximum subarray sum for right half part. - for(inti=middle+1; I<= right; i++) to { +Sum + =Nums[i]; -Rightsum =Integer.max (rightsum, sum); the } * $sum = 0;//reset the sum to 0.Panax Notoginseng - //calculate the maximum subarray sum for left half part. the for(intI=middle; I>= left; i--) + { ASum + =Nums[i]; theLeftsum =Integer.max (leftsum, sum); + } - $ //Choose the max between left and right at level. $ intres =Integer.max (LEFT_MSS, RIGHT_MSS); - //Choose the max between RES and middle range. - the returnInteger.max (res, leftsum +rightsum); - Wuyi } the -}
Resources:
Http://www.cnblogs.com/springfor/p/3877058.html
Https://www.youtube.com/watch?v=ohHWQf1HDfU
Leetcode algorithm Topic List-Leetcode algorithms Questions list
Leetcode 53. Maximum Subarray (largest sub-array)