-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmaximal_rectangle.java
56 lines (55 loc) · 1.76 KB
/
maximal_rectangle.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
class Solution {
public int maximalRectangle(char[][] matrix) {
int row = matrix.length;
if (row == 0)
return 0;
int col = matrix[0].length;
int max = 0;
int [][] dp = new int [row][col];
// calculate the height
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
if (i == 0) {
dp[i][j] = matrix[i][j] - '0';
}
else {
if (matrix[i][j] == '1')
dp[i][j] = dp[i - 1][j] + matrix[i][j] - '0';
}
}
}
for (int [] eachRow : dp) {
max = Math.max(max, largestRectangleHistogram(eachRow));
}
return max;
}
// same code for largest rectangle in a histogram
public int largestRectangleHistogram(int [] h) {
if (h == null || h.length == 0)
return 0;
int length = h.length;
int [] left = new int [length];
int [] right = new int [length];
int max = 0;
left[0] = -1;
right[length - 1] = length;
for (int i=1; i<length; i++) {
int currentIndex = i - 1;
while (currentIndex >= 0 && h[currentIndex] >= h[i]) {
currentIndex = left[currentIndex];
}
left[i] = currentIndex;
}
for (int i=length - 2; i>=0; i--) {
int currentIndex = i + 1;
while (currentIndex < length && h[currentIndex] >= h[i]) {
currentIndex = right[currentIndex];
}
right[i] = currentIndex;
}
for (int i=0; i<length; i++) {
max = Math.max(max, h[i] * (right[i] - left[i] - 1));
}
return max;
}
}