title : and the largest sub-sequence
Difficulty : Medium
topic content :
Given an integer array nums
, find the contiguous subarray (containing at least one number) which have the largest sum and return its sum.
translation :
Given an array of integers nums, find the adjacent sub-array (at least one number), the sum of which is the largest, and return it.
Example:
Input: [ -2,1,-3,4,-1,2,1,-5,4],output:6explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you had figured out the O (n) solution, try coding another solution using the divide and conquer approach, WHI CH is more subtle.
my train of thought : er, no good idea, only hard just two for, traverse all subsequence.
My Code :
1 Public intMaxsubarray (int[] nums) {2 intmax = Nums[0];3 for(inti = 0; i < nums.length; i++) {4 intsum = 0;5 for(intj = i; J < Nums.length; J + +) {6Sum + =Nums[j];7max = SUM > Max?Sum:max;8 }9 }Ten returnMax; One}
my degree of complexity : O (n2)
Coding Process Problems : (simple code Sometimes the problem is also quite a lot of, visible and can not be lazy, think of methods, even if it is very simple to write the line )
1. Start with Max's initial value of 0, and then found that when only a negative number will return 0, so when the maximum value of the traversal, max or Min's initial value should take the values inside the array ;
2, the initial value of sum is nums[i], J starts with i + 1, and then finds that the last element (also a subsequence) does not enter judgment, so when traversing all sub-sequences, J should start from I , and the initial value of sum takes 0;
Answer code :
1 Public intMaxsubarray (int[] nums) {2 intn =nums.length;3 int[] DP =New int[n];4Dp[0] = nums[0];5 intmax = Dp[0];6 7 for(inti = 1; I < n; i++){8Dp[i] = Math.max (Dp[i-1] +Nums[i], nums[i]);9Max =Math.max (Max, dp[i]);Ten } One A returnMax; -}
complexity of the answer : O (N)
The answer : Using the idea of dynamic programming, create a new array, using it to record the maximum value that can be reached by the sequence ending with nums[i] .
Take Nums[i] and Nums[i] + dp[i-1] The maximum value on the line (Dp[i-1] if greater than 0, then directly add, otherwise take nums[i] is the largest)
"Notice is not to say that dp[i] is the maximum value that a subsequence can achieve in a sequence ending with nums[i]"
So the largest value in the dp[] is the largest one required.
Optimization:
As long as you know the maximum value of DP, you do not need to create a DP, just use a variable mem to record the current, and then see if it is larger than Max:
1 Public intMaxsubarray (int[] nums) {2 intmax = Nums[0];3 intMEM =Max;4 for(inti = 1; i < nums.length; i++) {5MEM = Math.max (mem +Nums[i], nums[i]);6Max =Math.max (MEM, max);7 }8 returnMax;9}
Extension: What if I want to know the final position of the largest subsequence?
1 Public int[] Maxsubarray (int[] nums) {2 intn =nums.length;3 int[] DP =New intN [2];4Dp[0][0] = nums[0];5 int[] max = {Dp[0][0], 0, 0};6 7 for(inti = 1; I < n; i++){8 if(Dp[i-1][0] < 0) {9Dp[i][0] =Nums[i];TenDP[I][1] =i; One}Else { ADp[i][0] = Nums[i] + dp[i-1][0]; -DP[I][1] = dp[i-1][1]; - } the - if(Max[0] < dp[i][0]) { -Max[0] = dp[i][0]; -MAX[1] = dp[i][1]; +MAX[2] =i; - } + } A at returnMax; -}
Algorithm complexity: O (N)
Max[] Three elements are max value, start position, end position, respectively
The DP subscript is the terminating position, so the DP plus one dimension records the starting position corresponding to this terminating position dp[][]
"Note that the value of dp[i][1] will vary according to dp[i-1][0] < 0 "
Leetcode [53] (Java): Maximum subarray