Sum of maximal sub-arrays of annular two-dimensional arrays

Source: Internet
Author: User

Title: Returns the number of the largest sub-arrays in a two-dimensional integer array. Requirements: Enter a two-dimensional shape array with positive and negative numbers in the array. A two-dimensional array is connected to the end of the line, like a belt.

A contiguous integer or integers in an n array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Pair programming Requirements: The pair completes the programming task. One person is mainly responsible for program analysis, code programming.

One person is responsible for code review and Code test plan.

Publish a blog post about the process, experience, and how to resolve conflicts between two people (attach a work photo of the development).

Pair development process:

This time the programming is based on the last thought of the array, I and my partner @ the Angel RL Demon began a serious discussion, and then combined with the class discussion, how can be in a two-dimensional array on the basis of the original, coupled with the end-to-end condition, while reducing the complexity of time, The approximate idea of this method is: Iterate through the array of each number of the first number into the last number, the specific algorithm a[i][j-1]=a[i][j], so that it becomes a new two-dimensional array, output the maximum number of sub-arrays per array and then compare each output and, find the largest.

Program code:

1#include <iostream>2 using namespacestd;3 4 intMaxsubarray (int**a,intNintm)5 {6   int**p=New int*[n];7   inti,j;8   if(m==0|| n==0)9     return 0;Ten   //calculation P[i][j] One    for(i=0; i<n;i++) A   { -p[i]=New int[m]; -      for(j=0; j<m;j++) the     { -       if(i==0) -       { -         if(j==0) +p[i][j]=A[i][j]; -         Else +p[i][j]=p[i][j-1]+A[i][j]; A       } at       Else -       { -         if(j==0) -p[i][j]=p[i-1][j]+A[i][j]; -         Else -p[i][j]=p[i][j-1]+p[i-1][j]-p[i-1][j-1]+A[i][j]; in       } -     } to   } +   //computes the and of the largest subarray of two-dimensional arrays -   inttemp; the   intmax=a[0][0]; *   intans; $   //if M==1Panax Notoginseng   if(m==1) -   { the      for(i=0; i<n;i++) +     { A        for(j=i;j<n;j++) the       { +         if(i==0) -         { $temp=p[j][m-1]; $         } -         Else -         { thetemp=p[j][m-1]-p[i-1][m-1]; -         }Wuyi         if(ans<temp) theans=temp; -       } Wu     } -   } About   Else $   { -      for(i=0; i<n;i++) -     { -        for(j=i;j<n;j++) A       { +         if(i==0) the         { -temp=p[j][m-1]-p[j][m-2]; $         } the         Else the         { thetemp=p[j][m-1]-p[j][m-2]-p[i-1][m-1]+p[i-1][m-2]; the         } -          for(intk=m-2; k>=0; k--) in         { the           if(temp<0) thetemp=0; About           if(i==0) the           { the             if(k==0) thetemp+=P[j][k]; +             Else -temp+=p[j][k]-p[j][k-1]; the           }Bayi           Else the           { the             if(k==0) -temp+=p[j][k]-p[i-1][k]; -             Else thetemp+=p[j][k]-p[j][k-1]-p[i-1][k]+p[i-1][k-1]; the           } the           if(ans<temp) theans=temp; -         } the       } the     } the   }94   returnans; the } the  the intMain ()98 { About   intn,m,temp; -   inta1,a2;101   intk=0;102printf"Please enter the number of rows and columns of the two-dimensional array: \ n");103scanf"%d%d",&n,&m);104   inti,j; the   int**a=New int*[n];106printf"Please enter a%d*%d two-dimensional array element: \ n", n,m);107    for(i=0; i<n;i++)108   {109a[i]=New int[m]; the 111      for(j=0; j<m;j++) the     {113scanf"%d",&a[i][j]); the     } the   } the   intans=Maxsubarray (a,n,m);117printf"the sum of the largest sub-arrays of two-dimensional arrays is:%d\n", ans);118   for(a2=0; a2<m-1; a2++) 119{ for(i=0; i<n;i++) -{temp=a[i][0];121        for(j=1; j<m;j++)122{a[i][j-1]=a[i][j];}123a[i][m-1]=temp;124   } the    126      for(i=0; i<n;i++)127   {    -        for(j=0; j<m;j++)129       {    the          131           if(k%m==0)  the{cout<<Endl;}133cout<<a[i][j]<<" ";134k++;135       }136       137   }138   139a1=Maxsubarray (a,n,m); $printf"the sum of the largest sub-arrays of two-dimensional arrays is:%d\n", A1);141  }142   return 0;143}

Program run:

Summary experience:

Before the program design should be associated with an array of previous loops, we used the same method to solve the problem. The same is moved from the last column to the first column, the previous columns are moved backwards, there are n columns shifted n-1 times, the largest subarray of n matrices, and then the largest sub-array of the annular two-dimensional array is calculated.

Sum of maximal sub-arrays of annular two-dimensional arrays

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.