forked from havanagrawal/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_85.java
75 lines (66 loc) · 1.79 KB
/
_85.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 85. Maximal Rectangle
*
* Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
For example, given the following matrix:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 6.
*/
public class _85 {
public static class Solution1 {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) {
return 0;
}
int m = matrix.length;
int n = matrix[0].length;
int[] left = new int[n];
int[] right = new int[n];
int[] height = new int[n];
Arrays.fill(left, 0);
Arrays.fill(right, n);
Arrays.fill(height, 0);
int maxA = 0;
for (int i = 0; i < m; i++) {
int currLeft = 0;
int currRight = n;
//compute height, this can be achieved from either side
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
height[j]++;
} else {
height[j] = 0;
}
}
//compute left, from left to right
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
left[j] = Math.max(left[j], currLeft);
} else {
left[j] = 0;
currLeft = j + 1;
}
}
//compute right, from right to left
for (int j = n - 1; j >= 0; j--) {
if (matrix[i][j] == '1') {
right[j] = Math.min(right[j], currRight);
} else {
right[j] = n;
currRight = j;
}
}
//compute rectangle area, this can be achieved from either side
for (int j = 0; j < n; j++) {
maxA = Math.max(maxA, (right[j] - left[j]) * height[j]);
}
}
return maxA;
}
}
}