To find the maximum sub matrix and the maximum sub matrix

Source: Internet
Author: User

To find the maximum sub matrix: The implementation code is as follows

Package com.threeTop.www; The public class Maxsummatrix {/** * Gets the maximum and sub matrix of the matrix * @param matrix * @return/public static int Maxsumsubmatrix
	(int [][]matrix)
		{Int[][]total=matrix;
			for (int i=1;i<matrix.length;i++) {for (int j=0;j<matrix[0].length;j++) {total[i][j]+=total[i-1][j];
		} int maximum=integer.min_value; for (int i=0;i<matrix.length;i++) {for (int j=i;j<matrix.length;j++) {//result saved is from line I to J line known as element worthy and in
				
				t []result=new Int[matrix[0].length];
					for (int k=0;k<matrix[0].length;k++) {if (i==0) {result[k]=total[j][k];
					}else {result[k]=total[j][k]-total[i-1][k];
				
			    }//Call Fetch sub array and maximum function int Maximal=maxsubarray (result);
			    if (maximal>maximum) {maximum=maximal;
	}} return maximum;
		/** * Dynamic programming to find the largest sub array * @param result * @return/private static int Maxsubarray (int[] result) {int lastsum=0; int Maxsum=resulT[0];
			for (int i=0;i<result.length;i++) {if (lastsum>0) {//additive lastsum=lastsum+result[i];
			
			}else {///If a new starting point is required, the value of start Lastsum=result[i is assigned;
			
			} if (maxsum<lastsum) {maxsum=lastsum;
	} return maxsum;
		public static void Main (String[]args) {int [][]matrix={{-1,2,-1,3,-4},{2,3,4,-5,1},{1,-1,5,-3,-2}};
		SYSTEM.OUT.PRINTLN ("Maximum sub Matrix and for:");
		
	System.out.println (Maxsummatrix.maxsumsubmatrix (matrix));
 }
}

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.