-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max rectangle.java
55 lines (46 loc) · 1.55 KB
/
Max rectangle.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
52
53
54
55
// Max rectangle
/*
Given a binary matrix, Your task is to complete the function maxArea which the maximum size rectangle area in a binary-sub-matrix with all 1’s. The function takes 3 arguments the first argument is the Matrix M[ ] [ ] and the next two are two integers n and m which denotes the size of the matrix M. Your function should return an integer denoting the area of the maximum rectangle .
*/
class Test {
public int maxArea(int a[][],int m,int n){
int arr[] = new int[n];
int curr,next;
int val;
int max =0;
for(int row = 0;row<m;row++){
if(row==0){
for(int i=0;i<n;i++)
arr[i] =a[0][i];
}
else {
for(int i=0;i<n;i++){
if(a[row][i] == 0)
arr[i] = 0;
else
arr[i] =a[row][i] + arr[i];
}
}
val = hist(arr,n);
if(val>max)
max =val;
}
return max;
}
public static int hist(int arr[],int size){
int max = 0;
for(int j=0; j<size;j++){
int i=j;
int k=j;
int rec;
while(i>=0 && arr[j]<= arr[i])
i--;
while(k<=size-1 && arr[j]<=arr[k])
k++;
rec = arr[j] * (k-i-1);
if(rec >= max)
max = rec;
}
return max;
}
}