This problem is based on 695. Max Area of Island

1. Create a islands matrix, if a cell in grid is 1, there will be a corresponding island number in island matrix.

2. Go through grid, find out all islands (every island has a number) and their size and put the island number and size into a HashMap.

We do step1 and step2 together using DFS.

3. Go through grid again, for every cell which is 0, check its 4 directions's cell, to see whether they are in a group.

If yes, then add the group number. 

After all directions are checked, compare with the current max.

Note: because the 4 directions may be in the same group, so we need a HashSet to store the island number temporarily.

4. The res should be at least 1. If the res is 0, that means, all cells of grid are 1, in this case we need to return m*n.

Time complexity: O(m*n)

class Solution {
    private int m, n;
    private int[][] islands;
    private Map<Integer, Integer> map = new HashMap<>();
    private int islandNo = 0;
    private int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    private int res = 0;

    public int largestIsland(int[][] grid) {
        if (grid == null || grid.length == 0)
            return 0;
        m = grid.length;
        n = grid[0].length;
        islands = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && islands[i][j] == 0) {
                    islandNo++;
                    map.put(islandNo, dfs(grid, i, j));
                }
            }
        }

        connectGroups(grid);
        return res == 0 ? m * n : res;
    }

    private void connectGroups(int[][] grid) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    Set<Integer> islandNos = new HashSet<>();
                    int count = 1;
                    for (int[] dir : dirs) {
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (!checkCell(x, y))
                            continue;
                        if (!islandNos.contains(islands[x][y])) {
                            count += map.getOrDefault(islands[x][y], 0);
                            islandNos.add(islands[x][y]);
                        }
                    }
                    res = Math.max(res, count);
                }
            }
        }
    }

    private int dfs(int[][] grid, int i, int j) {
        int res = 0;
        if (!checkCell(i, j))
            return res;
        if (grid[i][j] == 1 && islands[i][j] == 0) {
            res = 1;
            islands[i][j] = islandNo;
            for (int[] dir : dirs) {
                res += dfs(grid, i + dir[0], j + dir[1]);
            }
        }
        return res;
    }

    private boolean checkCell(int x, int y) {
        if (x < 0 || x >= m || y < 0 || y >= n)
            return false;
        return true;
    }

}

 

posted on 2022-03-08 08:16  阳光明媚的菲越  阅读(24)  评论(0编辑  收藏  举报