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[][] = { { 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 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 islandstatic int maxAreaOfIsland(int n, int m, int grid[][]) {// base conditionif (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,leftstatic int dx[] = { -1, 0, 1, 0 };static int dy[] = { 0, 1, 0, -1 };static int count = 0;// funtion to check the// validation of the cellpublic static boolean isValid(int x, int y, int n, int m, int grid[][]) {// if cell is out of boundary// return falseif (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 falseif (visited[x][y] == 1 || grid[x][y] == 0)return false;// else return truereturn true;}// method to find the dfs traversal on 2d gridpublic static void dfs2Dgrid(int x, int y, int n, int m, int[][] grid) {// increase countcount++;// make the current cell as visitedvisited[x][y] = 1;// iterate for all the directionsfor (int i = 0; i < 4; i++) {int newx = dx[i] + x;int newy = dy[i] + y;// if valid and not visitedif (isValid(newx, newy, n, m, grid)) {// call dfs functiondfs2Dgrid(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 notbool isValid(int x,int y,vector<vector<int>> &grid){//if cell is not in the boundaryif(x<0||x>=n||y<0||y>=m)return false;//if cell is already visited or//thier is no islandif(vis[x][y]==1||grid[x][y]==0)return false;return true;}int cnt;//all for directionsint dx[4]={-1,0,1,0};int dy[4]={0,1,0,-1};//dfs for the 2d gridvoid dfs(int x,int y,vector<vector<int>> &grid){//mark the current cell as//visitedvis[x][y]=1;//increment the countcnt++;//iterate for all four directionsfor(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 islandint 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