Requirements:
Enter a one-dimensional shape array with positive and negative numbers in the array.
One-dimensional arrays end-to-end, like one end-to-end tape.
One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Time Complexity O (n)
Thinking Analysis :
First of all, humorous, the two methods in class I don't feel right.
The problem is basically the same as the last one, except that this one-dimensional array is connected to the ring. However, the sum of the maximal subarray is still to be obtained. To tell the truth, it's easier to do it in two cycles than to control it. may wish to change a way of thinking, think about, the last time to ask for a not the maximum value of the end-to-end, this back simply follow the idea of finding a minimum value, and then use the entire array and subtract this minimum value is done. The idea of finding the minimum and least number of non-end-to-end sub-arrays is no longer detailed, directly on the code.
1 intMain () {2System"title 08 Returns the maximum number of sub-arrays in an array of integers and");3cout <<"Digital Requirements: must have positive and negative numbers at the same time, there can be 0"<<Endl;4 intLen =0;5cout <<"input Array Length:";6CIN >>Len;7 int*a =New int[Len];8cout <<"input"<< Len <<"Number of:";9 for(inti =0; i < Len; ++i)TenCIN >>A[i]; One intsum =0; A intSUM1 =0; - intMin = a[0]; - for(inti =0; i < Len; ++i) { theSum1 + =A[i]; - if(sum + a[i] >=0) sum =0; - ElseSum + =A[i]; -min = min<sum?min:sum; + } -cout <<"the and of the largest sub-arrays:"<<sum1-min <<Endl; + return 0; A}
Verification :
Summary :
It's still a question of greedy arithmetic, but this time it turned a corner.
Returns the maximum number of sub-arrays in an array of integers and 2