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.)
  • Note:
The length of each dimension in the given grid does not exceed 50.
  • Examples:
[[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.
[[0,0,0,0,0,0,0,0]]

Given the above grid, return 0.
  • Analysis:
Use DFS to get every island's area and get the max area.
  • Code - Java:
class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        int row = grid.length;
        if (row == 0) {
            return 0;
        }
        int col = grid[0].length;
        if (col == 0) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 1) {
                    result = Math.max(result, helper(grid, i, j));
                }
            }
        }
        return result;
    }
    private int helper(int[][] grid, int x, int y) {
        if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == 0) {
            return 0;
        }
        grid[x][y] = 0;
        return 1 + helper(grid, x - 1, y) + helper(grid, x + 1, y) + helper(grid, x, y - 1) + helper(grid, x, y + 1); 
    }
}
  • Code - Java:
class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int row = grid.size();
        if (row == 0) {
            return 0;
        }
        int col = grid[0].size();
        if (col == 0) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 1) {
                    result = max(result, helper(grid, i, j));
                }
            }
        }
        return result;
    }
    int helper(vector<vector<int>> &grid, int x, int y) {
        if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size() || grid[x][y] == 0) {
            return 0;
        }
        grid[x][y] = 0;
        return 1 + helper(grid, x - 1, y) + helper(grid, x + 1, y) + 
              helper(grid, x, y - 1) + helper(grid, x, y + 1); 
    }
};
  • Code - Python:
class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        row = len(grid)
        if row == 0:
            return 0
        col = len(grid[0])
        if col == 0:
            return 0
        result = 0
        for i in range(row):
            for j in range(col):
                if grid[i][j] == 1:
                    result = max(result, self.helper(grid, i, j))
        return result

    def helper(self, grid, x, y):
        if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
            return 0
        grid[x][y] = 0
        return 1 + self.helper(grid, x - 1, y) + self.helper(grid, x + 1, y) + 
                 self.helper(grid, x, y - 1) + self.helper(grid, x, y + 1)
  • Code - C:
int helper(int** grid, int x, int y, int row, int col) {
    if (x < 0 || x >= row || y < 0 || y >= col || grid[x][y] == 0) {
        return 0;
    }
    grid[x][y] = 0;
    return 1 + helper(grid, x - 1, y, row, col) + helper(grid, x + 1, y, row, col) + 
        helper(grid, x, y - 1, row, col) + helper(grid, x, y + 1, row, col); 
}

int maxAreaOfIsland(int** grid, int gridRowSize, int gridColSize) {
    int result = 0;
    for (int i = 0; i < gridRowSize; i++) {
        for (int j = 0; j < gridColSize; j++) {
            int value = helper(grid, i, j, gridRowSize, gridColSize);
            if (result < value) {
                result = value;
            }
        }
    }
    return result;
}
  • Time Complexity: O(N^3)
  • Space Complexity: O(1)

results matching ""

    No results matching ""