Has formally begun to learn the data structure and algorithms, first learned the NetEase cloud class Zhejiang University data structure class, is Chen and He Chinming on, understand what is data structure and algorithm, learning some time space complexity analysis skills, combined with the previous sloppy mastery of the study, first from the simple topic to learn.
The topic is this:
Given a sequence of n integers, the child columns and the maximum values in each child column are evaluated.
Input: Enter a sequence of n integers
The output maximum sub-columns is required.
Example:
Input:
-2 -4 -5 -2
Output:
20
The difficulty of making this problem is not very large, at least it is easy to solve the violence, but the time complexity of the solution of violence is very large.
I wrote a brute-force solution using Python:
defMAXSUBSEQSUM1 (list):#O (n^3)Lenth = Len (list)Maxsum=0 forIinchRange (lenth): forJinchRange (lenth): Thissum=0 forCountinchRange (i,j+1): Thissum+=Lst[count]ifThissum >Maxsum:maxsum=thissum PrintMaxsum
The logic of the brute force solution is straightforward, which is to compare all the sub-columns and find them sequentially.
Since there is a triple nested loop, the execution time is three times the length of the input sequence size n, the time complexity is O (n^3) Obviously, this is a very poor algorithm, when the input is large, the results can not be obtained.
Let's analyze the process of this algorithm execution, and you can clearly see an improved place:
We let I as the first coordinate of the child column, j as the tail coordinate of the child column, increment successively.
Then count between I and J, ask for the and of all the sub-columns between I j, and compare the maximum values.
When I is 0, J is 0 o'clock, the value of lst[0] is obtained.
When I is 0, J is 1 o'clock, the value of lst[0], lst[0]+lst[1] is obtained.
When I is 0, J is 2 o'clock, the value of lst[0], lst[0]+lst[1], lst[0]+lst[1]+lst[2] is obtained.
...
It is easy to find a problem, we have repeatedly computed the previous value, that is, the last count of the For loop is meaningless at all, only to increase the time of the algorithm.
Of course, this is a deliberate addition to this question, which is not normally the case.
So, with the second algorithm, the cycle of count is removed:
def MAXSUBSEQSUM2 (list): #O (n^2) maxsum = 0 = len (list) for in Range (lenth): = 0 for in range (I, Lenth): + = list[j ]if thissum > maxsum := thissum return Maxsum
The time complexity of this algorithm is O (n^2), which is the most common solution for normal thought.
Generally a time complexity is O (n^2) problem, will want to change it to O (nlogn) problem.
Considering whether the method of divide and conquer is feasible, if divide-and-conquer method is adopted, the problem scale should be reduced.
The maximal sub-columns of a sequence, which is the largest sub-columns of the first 1/2 sequence, the largest sub-columns of the last 1/2 sequence, the largest sub-columns across the intermediate boundary, and the maximum value of three numbers. This is a constant separation, and it is natural to use separate treatment.
Examples are as follows:
A sequence is [-1, 2, 7,-3] The largest child of this sequence and is the largest of the following three numbers:
[-1, 2] The maximal sub-columns of this sequence, [7,-3] The largest sub-columns of the sequence, after 2, 7 the maximum and of the sequence of the boundary.
Similarly, [-1, 2] The maximal sub-columns of this sequence, is [ -1],[2], and the maximum of the sequence of -1,2 boundaries.
Decompose a problem into an easy-to-solve problem (the largest and most of the sequences that pass through the boundary) and two smaller original problems.
The largest and simplest of the sequences that pass through the boundary is equal to the largest sequence that traverses from the middle to the left, and the largest sequence that traverses to the right from the middle, and then adds the left and next largest values.
Here is the implementation code:
defMAXSUBSEQSUM3 (list):#O (NLOGN)Lenth =len (list)defmaxsum (list, left, right):ifleft = =Right :ifList[left] = =0:returnList[left]Else: return0Else: Center= Int ((left+right)/2) Maxleftsum=maxsum (list, left, center) Maxrightsum= Maxsum (list, center+1, right) maxleftbordersum=0 Leftbordersum=0 forIinchRange (center, left-1, 1): Leftbordersum+=List[i]ifLeftbordersum >Maxleftbordersum:maxleftbordersum=leftbordersum maxrightbordersum=0 Rightbordersum=0 forIinchRange (center+1, right+1): Rightbordersum+=List[i]ifRightbordersum >Maxrightbordersum:maxrightbordersum=RightbordersumreturnMax (Maxleftsum, maxrightsum, Maxleftbordersum+maxrightbordersum)returnMaxsum (list, 0, lenth-1)
The time complexity of the algorithm is not unfolded here, it is O (NLOGN)
In general an O (NLOGN) algorithm is good enough, but this problem actually has an O (n) algorithm, but also the fastest algorithm, because the data must be traversed to know the size:
def MAXSUBSEQSUM4 (list): #O (n) maxsum = 0 = len (list) = 0 for inch Range (lenth): + = list[i ]if thissum > maxsum := thissumelif Thissum < 0: = 0 return maxsum
This algorithm is easy to understand when it sees the code. Not detailed.
-------------------------------------------------
At the same time, in order to test these algorithms, we write a function and test function to generate the random tables to test the running time and result of each function:
ImportRandomImport Timedefmakeintseq (n, Low, high): List= [] forIinchrange (N): List.append (Random.randint (low,high))returnlist ....defTest (n, Low, high): LST=makeintseq (n, Low, high) forFcninch[maxsubseqsum4,maxsubseqsum3,maxsubseqsum2]: Start=time.clock () Num=FCN (LST) End=Time . Clock ()Print ' \ n%r:'% FCN.__name__ Print 'Num:%d'%NumPrint 'Time :', End-start
Because the first algorithm is too weak to test, in fact, when N is in the 1000, it waits for time to get the result. You can try execution time.
2015/10/13 algorithm Exercises: Maximum child columns and problems