Ring array maximum substring and maximum and minimum are relative

Source: Internet
Author: User

You know, the maximum and the minimum is relative, the sum minus the smallest can get the largest. The problem of programming beauty is not understood, and then reference to the http://zhangpeizhen.blog.163.com/blog/static/231873112201431784024921/

Two cases

1, ordinary array, can O (n) to find the maximum substring and.

2, if it is a ring, then to see across the n-1 to 0 of this ring of the situation, the requirement of the largest sum, only need to use the total minus the non-ring of the most small string and can.

Then take the maximum value of two cases.

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<string>using namespacestd;voiddp () {inti,t1,t2; T1= arra[0]; T2= arra[0]; Max=T1; Min=T2;  for(i =1; I < n; i + +)   {      if(T1 <=0) T1=Arra[i]; ElseT1+=Arra[i]; if(T2 >=0) T2=Arra[i]; ElseT2+=Arra[i]; Max=Max (T1,max); Min=min (t2,min); } Max= Max (max,total-Min); printf ("%d\n", Max (Max,0));} intMain () { while(~SCANF ("%d",&N)) { Total=0;  for(inti =0; I < n; i + +) {scanf ("%d",&Arra[i]); Total+=Arra[i];    } DP (); }   return 0;}

But I think less of a case, such as an array of all-1,

Normal array maximum substring and is-1,

In accordance with the largest ring of the above must be 0, but this is nothing to take the state,

So I think the last thing I need to decide is whether the most small string and the whole array and equal, if the equality is the case, then take the maximum substring of the normal array and the line.

Ring array maximum substring and maximum and minimum are relative

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.