Easy-to-understand Java DFS Solution, Just another Number of Island problem.


  • 1

    This question is just another Number of Island problem, only different is that you have to count the size of the each island you find, so set a count to get the size of island then compare with a globalmax (max), remember to reset it back to 0 every time when you find a new island.

    public int maxAreaOfIsland(int[][] grid) {
        if(grid.length == 0 || grid[0].length == 0){
            return 0;    
        }
        int m = grid.length, n = grid[0].length;
        int max = 0;
        int[] count = new int[1];
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 1){
                    count[0] = 0;
                    dfs(grid, i, j, m, n, count);       
                    max = Math.max(count[0], max);
                }
            }
        }
        return max;
    }
    
    private void dfs(int[][] grid, int i, int j, int m, int n, int[] count){
        if(i < 0 || j < 0 || i>= m || j >= n || grid[i][j] != 1){
            return;    
        }
        //marked visited;
        grid[i][j] = -1;
        count[0]++;
        dfs(grid, i + 1, j, m, n, count);
        dfs(grid, i - 1, j, m, n, count);
        dfs(grid, i, j + 1, m, n, count);
        dfs(grid, i, j - 1, m, n, count);
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.