Returns the maximum number of sub-arrays in an array of integers and 2

Source: Internet
Author: User
Tags array length

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

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.