-
Notifications
You must be signed in to change notification settings - Fork 112
/
Solution.java
49 lines (39 loc) · 1.21 KB
/
Solution.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
/**
* @author Oleg Cherednik
* @since 14.02.2019
*/
public class Solution {
public static void main(String... args) {
int[][] matrix = {
{1, 1, 0, 0},
{1, 0, 1, 1},
{1, 0, 1, 1},
{0, 1, 0, 0}};
System.out.println(getLargestAreaSet(matrix)); // 4
}
public static int getLargestAreaSet(int[][] matrix) {
int maxArea = 0;
int[] cache = new int[matrix[0].length];
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] == 0)
cache[col] = 0;
else
cache[col]++;
}
maxArea = Math.max(maxArea, inferArea(cache));
}
return maxArea;
}
private static int inferArea(int[] cache) {
int area = 0;
for (int i = 0; i < cache.length; i++) {
if (cache[i] == 0)
continue;
int min = cache[i];
for (int j = i; j < cache.length && cache[j] != 0; j++)
area = Math.max(area, Math.min(min, cache[j]) * (j - i + 1));
}
return area;
}
}