Skip to content

Commit a8f16c6

Browse files
refactor 85
1 parent 35cdd2f commit a8f16c6

File tree

1 file changed

+53
-67
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+53
-67
lines changed

src/main/java/com/fishercoder/solutions/_85.java

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,60 @@
22

33
import java.util.Arrays;
44

5-
/**
6-
* 85. Maximal Rectangle
7-
*
8-
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
9-
10-
For example, given the following matrix:
11-
12-
1 0 1 0 0
13-
1 0 1 1 1
14-
1 1 1 1 1
15-
1 0 0 1 0
16-
Return 6.
17-
18-
*/
195
public class _85 {
20-
public static class Solution1 {
21-
public int maximalRectangle(char[][] matrix) {
22-
if (matrix.length == 0) {
23-
return 0;
24-
}
25-
int m = matrix.length;
26-
int n = matrix[0].length;
27-
int[] left = new int[n];
28-
int[] right = new int[n];
29-
int[] height = new int[n];
30-
Arrays.fill(left, 0);
31-
Arrays.fill(right, n);
32-
Arrays.fill(height, 0);
33-
int maxA = 0;
34-
for (int i = 0; i < m; i++) {
35-
int currLeft = 0;
36-
int currRight = n;
37-
38-
//compute height, this can be achieved from either side
39-
for (int j = 0; j < n; j++) {
40-
if (matrix[i][j] == '1') {
41-
height[j]++;
42-
} else {
43-
height[j] = 0;
44-
}
45-
}
46-
47-
//compute left, from left to right
48-
for (int j = 0; j < n; j++) {
49-
if (matrix[i][j] == '1') {
50-
left[j] = Math.max(left[j], currLeft);
51-
} else {
52-
left[j] = 0;
53-
currLeft = j + 1;
54-
}
55-
}
56-
57-
//compute right, from right to left
58-
for (int j = n - 1; j >= 0; j--) {
59-
if (matrix[i][j] == '1') {
60-
right[j] = Math.min(right[j], currRight);
61-
} else {
62-
right[j] = n;
63-
currRight = j;
64-
}
65-
}
66-
67-
//compute rectangle area, this can be achieved from either side
68-
for (int j = 0; j < n; j++) {
69-
maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
6+
public static class Solution1 {
7+
public int maximalRectangle(char[][] matrix) {
8+
if (matrix.length == 0) {
9+
return 0;
10+
}
11+
int m = matrix.length;
12+
int n = matrix[0].length;
13+
int[] left = new int[n];
14+
int[] right = new int[n];
15+
int[] height = new int[n];
16+
Arrays.fill(left, 0);
17+
Arrays.fill(right, n);
18+
Arrays.fill(height, 0);
19+
int maxA = 0;
20+
for (int i = 0; i < m; i++) {
21+
int currLeft = 0;
22+
int currRight = n;
23+
24+
//compute height, this can be achieved from either side
25+
for (int j = 0; j < n; j++) {
26+
if (matrix[i][j] == '1') {
27+
height[j]++;
28+
} else {
29+
height[j] = 0;
30+
}
31+
}
32+
33+
//compute left, from left to right
34+
for (int j = 0; j < n; j++) {
35+
if (matrix[i][j] == '1') {
36+
left[j] = Math.max(left[j], currLeft);
37+
} else {
38+
left[j] = 0;
39+
currLeft = j + 1;
40+
}
41+
}
42+
43+
//compute right, from right to left
44+
for (int j = n - 1; j >= 0; j--) {
45+
if (matrix[i][j] == '1') {
46+
right[j] = Math.min(right[j], currRight);
47+
} else {
48+
right[j] = n;
49+
currRight = j;
50+
}
51+
}
52+
53+
//compute rectangle area, this can be achieved from either side
54+
for (int j = 0; j < n; j++) {
55+
maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
56+
}
57+
}
58+
return maxA;
7059
}
71-
}
72-
return maxA;
7360
}
74-
}
7561
}

0 commit comments

Comments
 (0)