-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMaximalRectangle.java
49 lines (48 loc) · 1.54 KB
/
MaximalRectangle.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
/*https://leetcode.com/problems/maximal-rectangle/*/
class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0) return 0;
int[] heights = new int[matrix[0].length];
int maxArea = -1;
for (int i = 0; i < matrix.length; ++i)
{
for (int j = 0; j < matrix[0].length; ++j)
{
if (matrix[i][j] == '0')
heights[j] = 0;
else ++heights[j];
}
int area = largestRectangleArea(heights);
maxArea = Math.max(area,maxArea);
}
return maxArea;
}
private int largestRectangleArea(int[] heights) {
int n = heights.length, largest = 0;
Stack<Integer> stack = new Stack<Integer>();
stack.push(-1);
for (int i = 0; i < n; ++i)
{
if (stack.peek() == -1 || heights[stack.peek()] <= heights[i])
{
stack.push(i);
largest = Math.max(heights[i],largest);
}
else
{
while (stack.peek() != -1 && heights[stack.peek()] >= heights[i])
{
int index = stack.pop();
largest = Math.max(largest,(i-stack.peek()-1)*heights[index]);
}
stack.push(i);
}
}
while (stack.peek() != -1)
{
int index = stack.pop();
largest = Math.max(largest,(n-stack.peek()-1)*heights[index]);
}
return largest;
}
}