Largest rectangle of 1's in 2d binary matrix

后端 未结 6 587
野性不改
野性不改 2020-12-07 17:56

There is a problem to find the maximum area of the 1 in the 0-1 matrix. In this problem there are two cases:

  1. area to be measure is of shape square. that\'s

6条回答
  •  伪装坚强ぢ
    2020-12-07 18:44

    **
    
    //use this dynamic programming approach
    //The problem can be reduced to finding the maximum rectangle area in a histogram, multiple times.
    After each row, you calculate the histogram built until that row, and that calculate the maximum area rectangle in that histogram.
    

    **

    import java.util.Scanner;
    
    
    public class LargestRectInAmatrix {
        static int row,col,matrix[][];
        static int maxArea=0;
        static int barMatrix[];
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            row=sc.nextInt();
            col=sc.nextInt();
            matrix=new int[row][col];
            barMatrix=new int[col];
            for(int i=0;imaxArea)
                {
                    maxArea=area;
                }
            }
    
        }
        private static int calculateArea(int l,int h)
        {
            if(l>h)
            {
                return Integer.MIN_VALUE;
            }
            if(l==h)
            {
                return barMatrix[l];
            }
            int u=calMinimumIndex(l,h);
            return (max(calculateArea(l, u-1),calculateArea(u+1, h),barMatrix[u]*(h-l+1)));
    
    
    
        }
        private static int max(int a,int b,int c)
        {
            if(a>b)
            {
                if(a>c)
                {
                    return a;
                }
                else
                    return c;
            }
            else
                if(b>c)
                {
                    return b;
                }
                else
                    return c;
        }
        private static int calMinimumIndex(int l,int h)
        {
            int min=Integer.MAX_VALUE;
            int min_index=0;
            for(int i=l;l<=h;i++)
            {
                if(barMatrix[i]

提交回复
热议问题