"Algorithm data structure Java implementation" Time complexity is O (n) Maximum and sequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.