There is a problem to find the maximum area of the 1 in the 0-1 matrix. In this problem there are two cases:
area to be measure is of shape square. that\'s
**
//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]