Skip to content

Commit 62c54ce

Browse files
Maximal Rectangle : Accepted
1 parent 715da20 commit 62c54ce

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ My accepted leetcode solutions to some of the common interview problems.
170170
- [Valid Parentheses](problems/src/stack/ValidParentheses.java) (Easy)
171171
- [Largest Rectangle In Histogram](problems/src/stack/LargestRectangleInHistogram.java) (Hard)
172172
- [Implement Queue using Stacks](problems/src/stack/MyQueue.java) (Easy)
173+
- [Maximal Rectangle](problems/src/stack/MaximalRectangle.java) (Hard)
173174

174175

175176
#### [String](problems/src/string)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 29/11/2017.
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+
Solution O(n * m): This problem is similar to LargestRectangleInHistogram.
19+
Run the largest rectangle in histogram algorithm for each row.
20+
*/
21+
public class MaximalRectangle {
22+
23+
/**
24+
* Main method
25+
* @param args
26+
* @throws Exception
27+
*/
28+
public static void main(String[] args) throws Exception{
29+
char[][] matrix = {{'1','0','1','0','0'}, {'1', '0', '1', '1', '1'}, {'1', '1', '1', '1', '1'},
30+
{'1', '0', '0', '1', '0'}};
31+
System.out.println(new MaximalRectangle().maximalRectangle(matrix));
32+
}
33+
34+
public int maximalRectangle(char[][] matrix) {
35+
if(matrix.length == 0 || matrix[0].length == 0) return 0;
36+
int[] A = new int[matrix[0].length];
37+
int max = Integer.MIN_VALUE;
38+
for(int i = 0; i < matrix.length; i ++){
39+
for(int j = 0; j < matrix[0].length; j++){
40+
if(matrix[i][j] == '1'){
41+
if(i > 0 && matrix[i - 1][j] == '1'){
42+
A[j] = A[j] + 1;
43+
} else{
44+
A[j] = 1;
45+
}
46+
} else {
47+
A[j] = 0;
48+
}
49+
}
50+
//calculate max rectangle for this row
51+
max = Math.max(max, getMaxRectangle(A));
52+
}
53+
return max;
54+
}
55+
56+
/**
57+
* Get max rectangle algorithm similar to max rectangle in histogram
58+
* @param heights
59+
* @return
60+
*/
61+
private int getMaxRectangle(int[] heights){
62+
int maxArea = Integer.MIN_VALUE;
63+
Stack<Integer> stack = new Stack<>();
64+
int i = 0;
65+
for (; i < heights.length; i++) {
66+
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
67+
int top = stack.pop();
68+
int base = stack.isEmpty() ? i : i - stack.peek() - 1;
69+
maxArea = Math.max(maxArea, base * heights[top]);
70+
}
71+
stack.push(i);
72+
}
73+
while (!stack.isEmpty()) {
74+
int top = stack.pop();
75+
int base = stack.isEmpty() ? i : i - stack.peek() - 1;
76+
maxArea = Math.max(maxArea, base * heights[top]);
77+
}
78+
return maxArea;
79+
}
80+
}

0 commit comments

Comments
 (0)