Returns the and of the largest interconnected subarray in a two-dimensional integer array

Source: Internet
Author: User

Program Design ideas: First, to analyze a two-dimensional array, you usually want to simplify it into a one-dimensional array. First, the largest subarray of each one-dimensional array is asked, and the subscript of the largest one-dimensional subarray of each row is noted. This will be divided into two cases: the first is the maximum number of sub-arrays between rows is connected, then can be directly added to get, the second is not connected, at this time, the largest sub-array of each row as a whole, and then to connect each of the largest array block, so that the minimum cost of its connection. Finally, we get the maximum of the number of interconnected sub-arrays.

Program Source code:

1  Packagemaximum sub-array;2 3 ImportJava.util.Scanner;4 5  Public classJudge2 {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9 Ten  One         intm,n,i,j,t2; AInteger smark=NewInteger (0); -Integer mmark=NewInteger (0); -  the         intsum; -  -         intup[]=New int[100]; -         intdown[]=New int[100]; +         intt[]=New int[100]; -         inta[][]=New int[100] [100]; +         intb[]=New int[100]; A              atSystem.out.println ("Please enter the number of rows and columns of the two-dimensional array:"); -Scanner scanner=NewScanner (system.in); -m=scanner.nextint (); -n=scanner.nextint (); -System.out.println ("Please enter this two-dimensional matrix:"); -          in          for(i=0;i<m;i++) -  to         { +  -              for(j=0;j<n;j++) the  *             { $ Panax Notoginsenga[i][j]=scanner.nextint (); -  the             } +  A         } the  +         //decompose a two-dimensional array into several one-dimensional arrays by rows -          for(i=0;i<m;i++) $  $         { -  -              for(j=0;j<n;j++) the  -             {Wuyi  theb[j]=A[i][j]; -  Wu             } -  Aboutsum=Max (N,b,smark,mmark); $  -up[i]=Smark;  -  -down[i]=Mmark; A  +t[i]=sum; the  -       $  the         } the  theT2=t[0]; the  -          for(i=0;i+1<m;i++) in  the         { the  About             if(Up[i]<=down[i+1] && down[i]>=up[i+1]) the  the             { the  +T2+=t[i+1]; -  the             }Bayi  the               for(j=up[i];j<up[i+1];j++) the  -             { -  the                 if(a[i+1][j]>0) t2+=a[i+1][j];//distinguishing independent positive numbers the  the             } the  -       the  the         } the 94 System.out.println (T2);  the     } the      Public Static intMax (intNinta[],integer Smark,integer Mmark) the 98     { About  -         intb[]=New int[100];101 102         inti,sum1= integer.min_value,max1=0;103         intJ=0;104          for(i=0;i<n+j;i++) the 106         {107 108             if(sum1<0)109  the             {111  thesum1=a[i%n];113j=i; the             } the  the             Else117 118             {119  -sum1=sum1+a[i%n];121 122             }123            124b[i]=sum1; the 126         }127  -Max1=b[0];129        the          for(i=0;i<n+j;i++)131  the         {133 134             if(max1<B[i])135 136              {137 138max1=B[i];139  $Mmark =i;141                 142 143              }144             145         }146 147           for(i = mmark;i >= 0;i--) 148 149         { Max 151             if(B[i] = =A[i]) the 153             {154 155Smark =i;156 157                   Break;158 159             } the 161         }162 163          returnmax1;164 165     }166 }167         

Program Run Result:

Returns the and of the largest interconnected subarray in a two-dimensional integer array

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.