Revised code from Number of Island


  • 0
    class Solution {
        int maxArea = 0;
        int area = 0;
        public int helper(int[][] grid, int x, int y, int m, int n){
            if(x <0 || y < 0 || x>=m || y >=n || grid[x][y] == 0){
                return area;
            }
            grid[x][y] = 0;
            area += 1;
            helper(grid, x-1,y,m,n);
            helper(grid, x,y-1,m,n);
            helper(grid, x+1,y,m,n);
            helper(grid, x, y+1,m,n);
            return area;
        }
        public int maxAreaOfIsland(int[][] grid) {
            int m = grid.length;
            if(m == 0){
                return 0;
            }
            int n = grid[0].length;
            for(int i = 0; i < m; i++){
                for(int j = 0; j < n; j++){
                    if(grid[i][j] == 1){
                        area = 0;
                        maxArea = Math.max(maxArea, helper(grid, i, j, m, n));
                    }
                }
            }
            return maxArea;
        }
    }
    

Log in to reply
 

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