-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumSizeRectangleOfAllOne.java
51 lines (50 loc) · 1.09 KB
/
MaximumSizeRectangleOfAllOne.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class MaximumSizeRectangleOfAllOne {
public static int MaxAreaHistogram(int[] hist,int n) {
int area=0;
int length=0,breadth=Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
if(hist[i]==0) {
length=0;
breadth=Integer.MAX_VALUE;
}
else {
length++;
breadth=Math.min(breadth, hist[i]);
area=Math.max(area, length*breadth);
}
}
return area;
}
public static int MaxSizeRectangle(int[][] arr,int row,int col) {
int area=0;
int[] hist=new int[col];
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(arr[i][j]==0) {
hist[j]=0;
}
else {
hist[j]+=arr[i][j];
}
}
area=Math.max(area,MaxAreaHistogram(hist,col));
}
return area;
}
public static void main(String[] args) {
int[][] arr;
int row,col;
Scanner sc=new Scanner(System.in);
row=sc.nextInt();
col=sc.nextInt();
arr=new int[row][col];
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
arr[i][j]=sc.nextInt();
}
}
System.out.println(MaxSizeRectangle(arr,row,col));
sc.close();
}
}