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:

Input:  [[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]]
Output: 6

Approach

Java

public class MaxAreaofIsland {
    static int visited[][] = null;

    public static void main(String[] args) {
        int grid[][] = { { 0010000100000 },
                 { 0000000111000 },
                { 0110100000000 },
                 { 0100110010100 },
                { 0100110011100 }, 
                0000000000100 },
                { 0000000111000 },
                 { 0000000110000 } };
        int n = grid.length;
        int m = grid[0].length;
        visited = new int[n][m];
        int area = maxAreaOfIsland(n, m, grid);
        System.out.println(area);
    }

    // method to find area of island
    static int maxAreaOfIsland(int nint mint grid[][]) {
        // base condition
        if (n == 0)
            return 0;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (visited[i][j] == 0 && grid[i][j] == 1) {
                    count = 0;
                    dfs2Dgrid(i, j, n, m, grid);
                    ans = Math.max(ans, count);
                }
            }
        }
        return ans;
    }

    // direction up,right,down,left
    static int dx[] = { -1010 };
    static int dy[] = { 010, -1 };
    static int count = 0;

    // funtion to check the
    // validation of the cell
    public static boolean isValid(int xint yint nint mint grid[][]) {
        // if cell is out of boundary
        // return false
        if (x < 0 || x >= n || y < 0 || y >= m)
            return false;
        // if the cell is alredy visited or
        // it is not grid[x][y]==0
        // return false
        if (visited[x][y] == 1 || grid[x][y] == 0)
            return false;
        // else return true
        return true;
    }

    // method to find the dfs traversal on 2d grid
    public static void dfs2Dgrid(int xint yint nint mint[][] grid) {
        // increase count
        count++;
        // make the current cell as visited
        visited[x][y] = 1;
        // iterate for all the directions
        for (int i = 0; i < 4; i++) {
            int newx = dx[i] + x;
            int newy = dy[i] + y;
            // if valid and not visited
            if (isValid(newx, newy, n, m, grid)) {
                // call dfs function
                dfs2Dgrid(newx, newy, n, m, grid);
            }
        }

    }

}

C++

#include <bits/stdc++.h>
using namespace std;


int n,m;
int vis[100][100];

//funtion to check the cell
//is  valid or not
bool isValid(int x,int y,vector<vector<int>> &grid)
{

    //if cell is not in the boundary
      if(x<0||x>=n||y<0||y>=m)
              return false;
      //if cell is already visited or
      //thier is no island
      if(vis[x][y]==1||grid[x][y]==0)
            return false;
      return true;
}
int cnt;

//all for directions
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

//dfs for the 2d grid
void dfs(int x,int y,vector<vector<int>> &grid)
{
    //mark the current cell as
    //visited
    vis[x][y]=1;

    //increment the count
    cnt++;
    //iterate for all four directions
      for(int i=0;i<4;i++)
      {
          if(isValid(dx[i]+x,dy[i]+y,grid))
          {
              dfs(x+dx[i],y+dy[i],grid);
          }
      }
}

//function to find the maximum area
//of island
int maxAreaOfIsland(vector<vector<int>>& grid
{
        n=grid.size();
        if(n==0)
              return 0;
        m=grid[0].size();
        int ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(vis[i][j]==0&&grid[i][j]==1)
                {
                    cnt=0;
                    dfs(i,j,grid);
                    ans=max(ans,cnt);
                }
            }
        }
        return ans;
}
int main()
{
  vector<vector<int>> grid={{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}};
  int area=maxAreaOfIsland(grid);
  cout<<area<<"\n";
  return 0;
}


No comments:

Post a Comment