##### Welcome to Subscribe On Youtube
• /**

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.

*/

public class Max_Area_of_Island {

class Solution {

int result = 0;

public int maxAreaOfIsland(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return result;
}

for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
result = Math.max(result, dfs(grid, i, j));
}
}
}

return result;
}

private int dfs(int[][] grid, int row, int col) {
if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] == 0) {
return 0;
}

// not keeping visited marking...
grid[row][col] = 0;

return 1 + dfs(grid, row + 1, col) + dfs(grid, row - 1, col) + dfs(grid, row, col + 1) + dfs(grid, row, col - 1);
}
}
}

############

class Solution {
private int[][] grid;
private int m;
private int n;

public int maxAreaOfIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}

private int dfs(int i, int j) {
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
int ans = 1;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
ans += dfs(x, y);
}
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/max-area-of-island/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& A) {
int M = A.size(), N = A[0].size(), dirs[4][2] = { {0,1},{0,-1},{-1,0},{1,0} }, ans = 0;
function<int(int, int)> dfs = [&](int i, int j) {
A[i][j] = 0;
int cnt = 1;
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] == 0) continue;
cnt += dfs(x, y);
}
return cnt;
};
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j] == 0) continue;
ans = max(ans, dfs(i, j));
}
}
return ans;
}
};

• class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(i, j):
grid[i][j] = 0
ans = 1
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
ans += dfs(x, y)
return ans

m, n = len(grid), len(grid[0])
return max(
[dfs(i, j) for i in range(m) for j in range(n) if grid[i][j] == 1],
default=0,
)

############

class Solution(object):
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
row, col = len(grid), len(grid[0])
def dfs(i, j):
if 0 <= i <= row - 1 and 0 <= j <= col - 1 and grid[i][j]:
grid[i][j] = 0 # 重要，防止再次搜索
return  1 + dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1)
return 0
return max(dfs(i, j) for i in xrange(row) for j in xrange(col))

• func maxAreaOfIsland(grid [][]int) int {
m, n := len(grid), len(grid[0])
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int) int
dfs = func(i, j int) int {
grid[i][j] = 0
ans := 1
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
ans += dfs(x, y)
}
}
return ans
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
ans = max(ans, dfs(i, j))
}
}
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

• function maxAreaOfIsland(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
let dfs = function (i, j) {
grid[i][j] = 0;
let ans = 1;
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
ans += dfs(x, y);
}
}
return ans;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}


• impl Solution {
fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
return 0;
}
grid[i][j] = 0;
let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
if i != 0 {
res += Self::dfs(grid, i - 1, j)
}
if j != 0 {
res += Self::dfs(grid, i, j - 1)
}
res
}

pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut res = 0;
for i in 0..m {
for j in 0..n {
res = res.max(Self::dfs(&mut grid, i, j))
}
}
res
}
}


• class Solution {
final int BLOCK = -1;
final int WHITE = 0;
final int GRAY = 1;
final int BLACK = 2;

public int maxAreaOfIsland(int[][] grid) {
int rows = grid.length, columns = grid[0].length;
int[][] colors = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (grid[i][j] == 0)
colors[i][j] = BLOCK;
}
}
int maxArea = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (colors[i][j] == WHITE) {
int area = breadthFirstSearch(grid, colors, i, j);
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}

public int breadthFirstSearch(int[][] grid, int[][] colors, int row, int column) {
int area = 0;
int rows = grid.length, columns = grid[0].length;
int[][] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
colors[row][column] = GRAY;
queue.offer(new int[]{row, column});
while (!queue.isEmpty()) {
int[] square = queue.poll();
int curRow = square[0], curColumn = square[1];
for (int[] direction : directions) {
int newRow = curRow + direction[0], newColumn = curColumn + direction[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && colors[newRow][newColumn] == WHITE) {
colors[newRow][newColumn] = GRAY;
queue.offer(new int[]{newRow, newColumn});
}
}
colors[curRow][curColumn] = BLACK;
area++;
}
return area;
}
}

############

class Solution {
private int[][] grid;
private int m;
private int n;

public int maxAreaOfIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}

private int dfs(int i, int j) {
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
int ans = 1;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k];
int y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
ans += dfs(x, y);
}
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/max-area-of-island/
// Time: O(MN)
// Space: O(MN)
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& A) {
int M = A.size(), N = A[0].size(), dirs[4][2] = { {0,1},{0,-1},{-1,0},{1,0} }, ans = 0;
function<int(int, int)> dfs = [&](int i, int j) {
A[i][j] = 0;
int cnt = 1;
for (auto &[dx, dy] : dirs) {
int x = i + dx, y = j + dy;
if (x < 0 || x >= M || y < 0 || y >= N || A[x][y] == 0) continue;
cnt += dfs(x, y);
}
return cnt;
};
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (A[i][j] == 0) continue;
ans = max(ans, dfs(i, j));
}
}
return ans;
}
};

• class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(i, j):
grid[i][j] = 0
ans = 1
for a, b in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
ans += dfs(x, y)
return ans

m, n = len(grid), len(grid[0])
return max(
[dfs(i, j) for i in range(m) for j in range(n) if grid[i][j] == 1],
default=0,
)

############

class Solution(object):
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
row, col = len(grid), len(grid[0])
def dfs(i, j):
if 0 <= i <= row - 1 and 0 <= j <= col - 1 and grid[i][j]:
grid[i][j] = 0 # 重要，防止再次搜索
return  1 + dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1)
return 0
return max(dfs(i, j) for i in xrange(row) for j in xrange(col))

• func maxAreaOfIsland(grid [][]int) int {
m, n := len(grid), len(grid[0])
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int) int
dfs = func(i, j int) int {
grid[i][j] = 0
ans := 1
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
ans += dfs(x, y)
}
}
return ans
}
ans := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == 1 {
ans = max(ans, dfs(i, j))
}
}
}
return ans
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

• function maxAreaOfIsland(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
let ans = 0;
const dirs = [-1, 0, 1, 0, -1];
let dfs = function (i, j) {
grid[i][j] = 0;
let ans = 1;
for (let k = 0; k < 4; ++k) {
const x = i + dirs[k];
const y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
ans += dfs(x, y);
}
}
return ans;
};
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans = Math.max(ans, dfs(i, j));
}
}
}
return ans;
}


• impl Solution {
fn dfs(grid: &mut Vec<Vec<i32>>, i: usize, j: usize) -> i32 {
if i == grid.len() || j == grid[0].len() || grid[i][j] == 0 {
return 0;
}
grid[i][j] = 0;
let mut res = 1 + Self::dfs(grid, i + 1, j) + Self::dfs(grid, i, j + 1);
if i != 0 {
res += Self::dfs(grid, i - 1, j)
}
if j != 0 {
res += Self::dfs(grid, i, j - 1)
}
res
}

pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut res = 0;
for i in 0..m {
for j in 0..n {
res = res.max(Self::dfs(&mut grid, i, j))
}
}
res
}
}