1. Background maximum sequence and problem has always been a relatively classic algorithm problem, see this problem, there are many ways to solve problems. Today we see a time-complexity algorithm for solving only O (n), which is recorded here. The idea is simple, for example, there are p1,p2,p3,p4 ... Such a sequence, we start from P1, for example, in P5 when the sum of less than 0, it can be determined. In the first case, the maximum sequence is between P1~P5, or between p6~pn. Because if P1+P2+......+P5 and less than 0, then it can be regarded as a number, and the first number of the sequence, then any one of the largest sequence will not contain this number.
2. Code implementation
Package Algorithm_analysis;public class Maxsumofarray {public static void Main (String args[]) { System.out.print (Max_sum ()); } public static int Max_sum () { int[] array={-2,11,-4,13,-5,-2}; int max_sum=0; int array_sum=0; for (int j=0;j<array.length;j++) { array_sum+=array[j]; if (array_sum<0) { max_sum=0; } if (array_sum>max_sum) { max_sum=array_sum; } } return max_sum;} }
Reference: http://blog.csdn.net/superchanon/article/details/8228212 (fully four implementations)
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
"Algorithm data structure Java implementation" Time complexity is O (n) Maximum and sequence