forked from mengli/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximal Rectangle.java
More file actions
51 lines (50 loc) · 1.6 KB
/
Maximal Rectangle.java
File metadata and controls
51 lines (50 loc) · 1.6 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
39
40
41
42
43
44
45
46
47
48
49
50
51
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.
public class Solution {
public int maximalRectangle(char[][] matrix) {
int row = matrix.length;
if (row == 0) return 0;
int col = matrix[0].length;
int[][] map = new int[row][col];
int area = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int prev = 0;
if (i != 0) {
prev = map[i - 1][j];
}
if (matrix[i][j] == '1') {
map[i][j] = prev + 1;
} else {
map[i][j] = prev;
}
}
}
for (int i = 0; i < row; i++) {
for (int j = i; j < row; j++) {
int[] line = new int[col];
for (int k = 0; k < col; k++) {
line[k] = map[j][k] - map[i][k] + (matrix[i][k] == '0' ? 0 : 1);
}
int l = 0;
int tmp = 0;
for (int f = 0; f < col; f++) {
if (line[f] == j - i + 1) tmp++;
else {
if (tmp > l) {
l = tmp;
}
tmp = 0;
}
}
if (tmp > l) {
l = tmp;
}
int s = (j - i + 1) * l;
if (s > area) {
area = s;
}
}
}
return area;
}
}