from typing import List
def maxAreaOfIsland(grid: List[List[int]]) -> int:
rows, cols = len(grid), len(grid[0])
visited = set()
max_area = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1 and (r, c) not in visited:
stack = [(r, c)]
area = 0
while stack:
row, col = stack.pop()
if (row, col) in visited:
continue
visited.add((row, col))
area += 1
for nr, nc in [(row+1,col),(row-1,col),(row,col+1),(row,col-1)]:
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == 1 and (nr, nc) not in visited:
stack.append((nr, nc))
max_area = max(max_area, area)
return max_area
# Example usage:
if __name__ == '__main__':
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]]
print(maxAreaOfIsland(grid))
Line Notes
stack = [(r, c)]Initialize stack with starting land cell for iterative DFS
while stack:Loop until all connected land cells are explored
if (row, col) in visited:Skip already visited cells to avoid cycles
for nr, nc in [(row+1,col),(row-1,col),(row,col+1),(row,col-1)]:Check all four neighbors for exploration
max_area = max(max_area, area)Update maximum island area found so far
import java.util.*;
public class MaxAreaOfIsland {
public static int maxAreaOfIsland(int[][] grid) {
int rows = grid.length, cols = grid[0].length;
boolean[][] visited = new boolean[rows][cols];
int maxArea = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 1 && !visited[r][c]) {
Stack<int[]> stack = new Stack<>();
stack.push(new int[]{r, c});
int area = 0;
while (!stack.isEmpty()) {
int[] cell = stack.pop();
int row = cell[0], col = cell[1];
if (visited[row][col]) continue;
visited[row][col] = true;
area++;
int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};
for (int[] d : directions) {
int nr = row + d[0], nc = col + d[1];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1 && !visited[nr][nc]) {
stack.push(new int[]{nr, nc});
}
}
}
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}
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}
};
System.out.println(maxAreaOfIsland(grid));
}
}
Line Notes
Stack<int[]> stack = new Stack<>();Use stack to manage cells for iterative DFS
while (!stack.isEmpty()) {Process all connected cells until stack empties
if (visited[row][col]) continue;Skip cells already visited to avoid infinite loops
int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};Define neighbor directions for exploration
maxArea = Math.max(maxArea, area);Update max area after exploring one island
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int rows = grid.size(), cols = grid[0].size();
vector<vector<bool>> visited(rows, vector<bool>(cols, false));
int maxArea = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 1 && !visited[r][c]) {
stack<pair<int,int>> stk;
stk.push({r, c});
int area = 0;
while (!stk.empty()) {
auto [row, col] = stk.top();
stk.pop();
if (visited[row][col]) continue;
visited[row][col] = true;
area++;
vector<pair<int,int>> directions = {{1,0},{-1,0},{0,1},{0,-1}};
for (auto& d : directions) {
int nr = row + d.first, nc = col + d.second;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1 && !visited[nr][nc]) {
stk.push({nr, nc});
}
}
}
maxArea = max(maxArea, area);
}
}
}
return maxArea;
}
};
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}
};
Solution sol;
cout << sol.maxAreaOfIsland(grid) << endl;
return 0;
}
Line Notes
stack<pair<int,int>> stk;Use stack to hold cells for iterative DFS
while (!stk.empty()) {Process all connected land cells iteratively
if (visited[row][col]) continue;Skip already visited cells to avoid loops
vector<pair<int,int>> directions = {{1,0},{-1,0},{0,1},{0,-1}};Define neighbor directions for exploration
maxArea = max(maxArea, area);Update max island area after exploration
function maxAreaOfIsland(grid) {
const rows = grid.length, cols = grid[0].length;
const visited = new Set();
let maxArea = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1 && !visited.has(r + ',' + c)) {
const stack = [[r, c]];
let area = 0;
while (stack.length > 0) {
const [row, col] = stack.pop();
if (visited.has(row + ',' + col)) continue;
visited.add(row + ',' + col);
area++;
const directions = [[1,0],[-1,0],[0,1],[0,-1]];
for (const [dr, dc] of directions) {
const nr = row + dr, nc = col + dc;
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] === 1 && !visited.has(nr + ',' + nc)) {
stack.push([nr, nc]);
}
}
}
maxArea = Math.max(maxArea, area);
}
}
}
return maxArea;
}
// Example usage:
const 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]
];
console.log(maxAreaOfIsland(grid));
Line Notes
const stack = [[r, c]];Initialize stack with starting land cell for iterative DFS
while (stack.length > 0) {Process all connected land cells until stack empties
if (visited.has(row + ',' + col)) continue;Skip already visited cells to avoid infinite loops
const directions = [[1,0],[-1,0],[0,1],[0,-1]];Define neighbor directions for exploration
maxArea = Math.max(maxArea, area);Update maximum island area found so far