A two-dimensional array returns the sum of the maximal sub-matrices

Source: Internet
Author: User

Design ideas

Converting a two-dimensional array into an array (not a formal conversion) is calculated by merging successive rows and then calculating the maximum number of sub-arrays for the last written one-dimensional array. Time complexity does not meet the required O(n), but also by traversing the way a sub-matrix and the previous sub-matrices are added if less than 0 then the sub-matrix is taken down, and the matrix after this sub-matrix is computed, and the maximum value of the traversal is known.

Code:

Package Soft_third_test;public class Test {static int maxsum (int p[][],int startline,int endline,int n) {int ans=p[endline ][1]-p[startline][1];int cmax=ans;for (int i=2;i<=n;i++) {int Ci=p[endline][i]-p[startline][i];cmax=math.max ( Cmax+ci, CI); Ans=math.max (Cmax, ans);} System.out.println (startline+ "" +endline+ "" +ans "; return ans;} Static int[][] Colsum (int arr[][]) {int m=arr.length;int n=arr[0].length;int p[][]=new int[m+1][n+1];for (int i=1;i<= m;i++) for (int j=1;j<=n;j++) P[i][j]=p[i-1][j]+arr[i-1][j-1];return p;} static int maxarrsum (int arr[][]) {int m=arr.length;int n=arr[0].length;if (m>n) {Arr=reversearr (arr); int tmp=m;m=n; N=tmp;} int p[][]=colsum (arr); int tempmax=0;//h represents the number of rows of the current matrix, which is how many rows are treated as a row for (int h=1;h<=m;h++) for (int i=1;i+h-1<=m;i++) {int endline=i+h-1;//is converted to a number with a length of n, and the complexity is O (n) tempmax=math.max (Tempmax, Maxsum (P,i,endline,n));} return Tempmax;} Static int[][] Reversearr (int[][] arr) {//TODO auto-generated method stubint m=arr.length;int n=arr[0].length;int NEWARR [][]=new int[n][m];for (int i=0;i<m;i++) for (int j=0;j<n;j++) Newarr[j][i]=arr[i][j];return newArr;} public static void Main (string[] args) {//TODO auto-generated method stub int arr[][]={{1,2,-51,-4,-50},{-8,-3,4,255,1},    {3,8,104,1,3},{-4,-1,10,7,-6}};int ans=maxarrsum (arr); System.out.print ("ddd"); System.out.println (ANS);}}

Results

Summarize:

In general, the algorithm is quite complex, at the beginning of the thought and the last one-dimensional array must have been linked but there is no design ideas. The method was finally written through a few days of online reference and discussions with Guo Hao students. This time I was in the process of writing, feeling very tired, but at the time of completion, our team (although only two people) feel very excited about it. Feel that our level is still a low, do not be embarrassed to argue with teammates, each time the idea of collision may produce a more perfect idea, and the knot group can let us improve ourselves to find their own shortcomings. At the same time also want to learn more on the Internet, we now have the network this thing is not a waste of medicine. In addition, the previous several times are written in C + + Java are quickly forget clean, should be in time to review those basic things.

Team Photo:

A two-dimensional array returns the sum of the maximal sub-matrices

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.