-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal_Rectangle.java
More file actions
38 lines (36 loc) · 1.13 KB
/
Maximal_Rectangle.java
File metadata and controls
38 lines (36 loc) · 1.13 KB
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
import java.util.*;
import java.io.*;
import java.lang.*;
public class Maximal_Rectangle {
class Solution {
public int maximalRectangle(char[][] matrix)
{
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
{
return 0;
}
int rows = matrix.length;
int cols = matrix[0].length;
int[] heights = new int[cols + 1];
int maxarea = 0;
for(char[] row : matrix)
{
for (int i = 0; i < cols; i++)
{
heights[i] = (row[i] == '1') ? heights[i] + 1 : 0;
}
int n = heights.length;
for (int i = 0; i < n; i++)
{
for (int j = i, minHeight = Integer.MAX_VALUE; j < n; j++)
{
minHeight = Math.min(minHeight, heights[j]);
int area = minHeight * (j - i + 1);
maxarea = Math.max(maxarea, area);
}
}
}
return maxarea;
}
}
}