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
| class Solution {
public static Integer maxArea(List<List<Integer>> data) { int m = data.size(), n = data.get(0).size(), max = 0; for (List<Integer> list : data) { n = Math.max(n, list.size()); } int[][] arr = new int[m][n]; List<int[]> starts = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { for (int j = 0; j < data.get(i).size(); j++) { if (data.get(i).get(j) != 0) { arr[i][j] = 1; starts.add(new int[]{i, j}); } } } for (int[] start : starts) { max = Math.max(max, search(arr, start[0], start[1])); } return max; }
public static Integer search(int[][] arr, int row, int col) { if (row < 0 || row >= arr.length || col < 0 || col >= arr[0].length || arr[row][col] == 0) { return 0; } int count = 1; arr[row][col] = 0; count += search(arr, row - 1, col); count += search(arr, row, col + 1); count += search(arr, row + 1, col); count += search(arr, row, col - 1); return count; }
public static void main(String[] args) { List<List<Integer>> test = new ArrayList<>(); test.add(Arrays.asList(1, 0, 0, 1, 0)); test.add(Arrays.asList(1, 0, 1)); test.add(Arrays.asList(0, 0, 1, 0, 1)); test.add(Arrays.asList(1, 0, 1, 0, 1)); test.add(Arrays.asList(1, 0, 1, 1)); System.out.println(maxArea(test)); } }
|