Saturday, October 7, 2017

695. Max Area of Island


void dfs(vector<vector<int>>& grid, int i, int j, int& area) { int NI = grid.size(); int NJ = grid[0].size(); if(i<0 || i>=NI || j<0 || j>=NJ) return; if(grid[i][j] == 0) return; grid[i][j] = 0; area++; dfs(grid, i, j+1, area); dfs(grid, i, j-1, area); dfs(grid, i+1, j, area); dfs(grid, i-1, j, area); } int maxAreaOfIsland(vector<vector<int>>& grid) { if(grid.size() == 0) return 0; int NI = grid.size(); int NJ = grid[0].size(); int mx = 0; for(int i=0; i<NI; i++) { for(int j=0; j<NJ; j++) { if(grid[i][j]) { int area = 0; dfs(grid, i, j, area); if(area>mx) mx = area; } } } return mx; }

No comments:

Post a Comment