forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumRectangleOf1sInMatrix.java
79 lines (67 loc) · 2.78 KB
/
MaximumRectangleOf1sInMatrix.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
76
77
78
79
package com.rampatra.dynamicprogramming;
import com.rampatra.stacks.MaxRectangleAreaInHistogram;
/**
* Given a 2D matrix of 0s and 1s. Find the largest rectangle of all 1s in this matrix.
* <p>
* Level: Hard
* Time Complexity: O(rows * cols)
* Space Complexity: O(cols)
* <p>
* Note: If the number of cols is too large as compared to rows then you can process the matrix column-wise and create
* the histogram for each column. In this way the hist[] array will be of size = number of rows in the matrix.
*
* @author rampatra
* @since 2019-04-05
*/
public class MaximumRectangleOf1sInMatrix {
private static int getMaxRectangleSizeOf1s(int[][] binaryMatrix) {
int area;
int maxArea = 0;
int[] hist = new int[binaryMatrix[0].length];
/*
Create a histogram with the rows. Start with the first row, create a histogram and then extend this
histogram based on the elements in the next rows. If the element in the row is a 0 then make the bar in
the histogram 0 or else just increase the bar in the histogram.
Basically, we are creating a histogram with all the 1s in the matrix and then finding the maximum
rectangle size of this histogram.
*/
for (int row = 0; row < binaryMatrix.length; row++) {
for (int col = 0; col < binaryMatrix[0].length; col++) {
if (binaryMatrix[row][col] == 0) {
hist[col] = 0;
} else {
hist[col] += binaryMatrix[row][col];
}
}
area = MaxRectangleAreaInHistogram.getMaxRectangleArea(hist);
maxArea = Math.max(maxArea, area);
}
return maxArea;
}
public static void main(String[] args) {
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{0, 1, 1},
{0, 0, 1},
{0, 1, 1}}));
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{0, 1, 1, 1, 0},
{0, 0, 1, 1, 0},
{0, 1, 1, 1, 0}}));
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{1, 1, 1, 0},
{1, 1, 1, 1},
{0, 1, 1, 0},
{0, 1, 1, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}}));
// edge cases
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{}}));
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{0}}));
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{0, 0, 0}}));
System.out.println(getMaxRectangleSizeOf1s(
new int[][]{{1}}));
}
}