Max Area of Island

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]

Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:

[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Note: The length of each dimension in the given grid does not exceed 50.

Solution:

Union Find

class Solution {
    static class UF {
        int[] sizes;
        int[] parents;
        int maxSize = 0;
        
        public UF(int[][] grid, int m, int n) {
            int len = m * n;
            sizes = new int[len + 1];
            parents = new int[len + 1];
            for (int i = 0; i <= len; i ++) {
                parents[i] = i;
            }
            for (int i = 0; i < m; i ++) {
                for (int j = 0; j < n; j ++) {
                    if (grid[i][j] == 1) {
                        sizes[i * n + j] = 1;
                        maxSize = 1;
                    }
                }
            }
        }
        
        public int find(int x) {
            while(parents[x] != x) {
                parents[x] = parents[parents[x]];
                x = parents[x];
            }
            return x;
        }
        
        public void union(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) return;
            if (sizes[rootX] <= sizes[rootY]) {
                sizes[rootX] += sizes[rootY];
                parents[rootY] = rootX;
                maxSize = Math.max(maxSize, sizes[rootX]);
            } else {
                sizes[rootY] += sizes[rootX];
                parents[rootX] = parents[rootY];
                maxSize = Math.max(maxSize, sizes[rootY]);
            }
        }
    }
    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        if (m == 0) return 0;    
        int n = grid[0].length;
        if (n == 0) return 0;
        UF islands = new UF(grid, m, n);
        int[] dx = new int[]{0,1,0,-1};
        int[] dy = new int[]{1,0,-1,0};
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                if (grid[i][j] == 1) {
                    for (int k = 0; k < 4; k ++) {
                        int nx = i + dx[k];
                        int ny = j + dy[k];
                        if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == 1) {
                            islands.union(i * n + j, nx * n + ny);
                        }
                    }
                }
            }
        }
        return islands.maxSize;
    }
}

DFS

class Solution {
    private int dfs(int[][] grid, boolean[][] visited, int i, int j, int curSize, int[] max) {
        int m = grid.length;
        int n = grid[0].length;
        int[] dx = new int[]{0,1,0,-1};
        int[] dy = new int[]{1,0,-1,0};
        curSize ++;
        max[0] = Math.max(max[0], curSize);
        for (int k = 0; k < 4; k ++) {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] == 1 && !visited[nx][ny]) {
                visited[nx][ny] = true;
                curSize = dfs(grid, visited, nx, ny, curSize, max);
            }
        }
        grid[i][j] = curSize;
        return curSize;
    }
    
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        if (m == 0) return 0;    
        int n = grid[0].length;
        if (n == 0) return 0;
        int[] max = new int[1];
        boolean[][] visited = new boolean[m][n];
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                if (grid[i][j] == 1 && !visited[i][j]) {
                    visited[i][j] = true;
                    grid[i][j] = dfs(grid, visited, i, j, 0, max);
                }
            }
        }
        return max[0];
    }
}