ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

827. Making A Large Island

2022-03-08 08:34:19  阅读:182  来源: 互联网

标签:return int res private Large grid islands Making 827


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;
    }

}

 

标签:return,int,res,private,Large,grid,islands,Making,827
来源: https://www.cnblogs.com/feiflytech/p/15978944.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有