Topic:
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 [4,?1,2,1] has the largest sum = 6.
More Practice:
If you had figured out of the O (n) solution, try coding another solution using the divide and conquer approach, which is mor E subtle.
Train of thought: This problem uses the general idea to traverse all results, the algorithm complexity is O (N2). The practice of online predecessors on this topic is mostly called dynamic programming method. (Personal feeling algorithm in the so-called dynamic programming and traditional mathematical dynamic planning is still a bit different, about the dynamic planning of the introduction see: Dynamic Planning) on the Internet, a senior said that the dynamic programming problem is the most important to find out the dynamic programming recursive. The solution of dynamic programming is to maintain two variables, local optimal and global optimal, and then solve the problem by using the recursive relation between local and global optimal variables.
In order to visually see the solution of the problem, we first look at an example (this example is from the "sword Point Offer"), the following table in the form of the solution of the process (the table is also excerpted from the "sword Point offer").
As you can see, the solution uses two storage variables, one that holds the largest sub-array of the global and the Globalmax, a sub-array that holds the local accumulation, and the Localmax.
When the first data is reached, if a[i] is positive, then localmax+a[i] must be larger, greater than Globalmax, and then the result of Localmax+a[i] is saved to Globalmax. If a[i] is negative, then localmax+a[i] must be smaller, less than Globalmax, and localmax+a[i] may be less than a[i] (as in step 3) then we will change the value of Localmax to A[i], which is Max (Localmax +a[i], A[i]).
Organize your thoughts:
Localmax = Max (Localmax+a[i], a[i])
globalmax= Max (GlobalMax, Localmax)
In fact, for A[i]>0, Max (Localmax+a[i], a[i]) is definitely the result of Localmax+a[i], and Max (GlobalMax, Localmax) is definitely localmax; when a[i]< At 0, the results of Max (Localmax+a[i], a[i]) will vary according to A[i], and the result of Max (GlobalMax, Localmax) is definitely globalMax.
C + + code:
class Solution{public: intmaxSubArray(intint n) { if0return0; int globalMax = A[0]; int localMax = A[0]; for (int1; i < n; i++) { localMax = max(localMax + A[i], A[i]); globalMax = max(localMax, globalMax); } return globalMax; }};
C # code:
class Solution{public intMaxsubarray(int[] A){if(A. Length <= 0)return 0; int GlobalMax =A[0]; int Localmax =A[0]; For(int i = 1; I < A. Length; I+ +){Localmax =Math.Max(Localmax + a[i], a[i]); GlobalMax =Math.Max(globalMax, localmax); } return GlobalMax; }}
Python code:
class solution: # @param A, a list of integers # @return An integer def Maxsubarray(self, A):size = Len (A)ifSize <=0:return 0GlobalMax = a[0] Localmax = a[0] forIinchRange1, size): Localmax = max (Localmax + a[i], a[i]) GlobalMax = max (GlobalMax, Localmax)returnGlobalMax
First write to this, later on the dynamic planning has a deep understanding, and then to add! Welcome Children's shoes criticism, learn from each other!
Another way of writing this question:
Class solution{ Public:int Maxsubarray(intA[],intN) {if(N <=0)return 0;intGlobalMax = a[0];intLocalmax = a[0]; for(inti =1; I < n; i++) {if(Localmax <0) Localmax = A[i];ElseLocalmax + = A[i];if(Localmax > GlobalMax) globalMax = Localmax; }returnGlobalMax; }};
Leetcode:maximum Subarray