Skip to content

Commit b038d8b

Browse files
refactor 85
1 parent 25cca29 commit b038d8b

File tree

1 file changed

+51
-46
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+51
-46
lines changed

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

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.Arrays;
44

55
/**
6+
* 85. Maximal Rectangle
7+
*
68
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
79
810
For example, given the following matrix:
@@ -12,59 +14,62 @@
1214
1 1 1 1 1
1315
1 0 0 1 0
1416
Return 6.
17+
1518
*/
1619
public class _85 {
20+
public static class Solution1 {
1721
public int maximalRectangle(char[][] matrix) {
18-
if (matrix.length == 0) {
19-
return 0;
20-
}
21-
int m = matrix.length;
22-
int n = matrix[0].length;
23-
int[] left = new int[n];
24-
int[] right = new int[n];
25-
int[] height = new int[n];
26-
Arrays.fill(left, 0);
27-
Arrays.fill(right, n);
28-
Arrays.fill(height, 0);
29-
int maxA = 0;
30-
for (int i = 0; i < m; i++) {
31-
int currLeft = 0;
32-
int currRight = n;
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;
3337

34-
//compute height, this can be achieved from either side
35-
for (int j = 0; j < n; j++) {
36-
if (matrix[i][j] == '1') {
37-
height[j]++;
38-
} else {
39-
height[j] = 0;
40-
}
41-
}
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+
}
4246

43-
//compute left, from left to right
44-
for (int j = 0; j < n; j++) {
45-
if (matrix[i][j] == '1') {
46-
left[j] = Math.max(left[j], currLeft);
47-
} else {
48-
left[j] = 0;
49-
currLeft = j + 1;
50-
}
51-
}
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+
}
5256

53-
//compute right, from right to left
54-
for (int j = n - 1; j >= 0; j--) {
55-
if (matrix[i][j] == '1') {
56-
right[j] = Math.min(right[j], currRight);
57-
} else {
58-
right[j] = n;
59-
currRight = j;
60-
}
61-
}
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+
}
6266

63-
//compute rectangle area, this can be achieved from either side
64-
for (int j = 0; j < n; j++) {
65-
maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
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]);
6770
}
68-
return maxA;
71+
}
72+
return maxA;
6973
}
74+
}
7075
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy