static const int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static void sink(int **grid, int r, int c, int rows, int cols, int *count) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] != 1)
return;
grid[r][c] = 0;
(*count)++;
for (int d = 0; d < 4; d++)
sink(grid, r + dirs[d][0], c + dirs[d][1], rows, cols, count);
}
int maxAreaOfIsland(int **grid, int gridSize, int *gridColSize) {
if (gridSize == 0)
return 0;
int mx = 0, count = 0;
int rows = gridSize, cols = gridColSize[0];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] == 1) {
count = 0;
sink(grid, i, j, rows, cols, &count);
if (count > mx)
mx = count;
}
return mx;
}
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>> &grid) {
if (grid.empty())
return 0;
int mx = 0, count = 0;
int rows = grid.size(), cols = grid[0].size();
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] == 1) {
count = 0;
sink(grid, i, j, rows, cols, count);
mx = max(count, mx);
}
return mx;
}
private:
static constexpr int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void sink(vector<vector<int>> &grid, int r, int c, int rows, int cols,
int &count) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] != 1)
return;
grid[r][c] = 0;
count++;
for (auto [dr, dc] : dirs)
sink(grid, r + dr, c + dc, rows, cols, count);
}
};
package graphs
var dirs = [4][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
func maxAreaOfIsland(grid [][]int) int {
if len(grid) == 0 {
return 0
}
mx, count := 0, 0
rows, cols := len(grid), len(grid[0])
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
if grid[i][j] == 1 {
count = 0
sink(grid, i, j, rows, cols, &count)
mx = max(count, mx)
}
}
}
return mx
}
func sink(grid [][]int, r, c, rows, cols int, count *int) {
if r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] != 1 {
return
}
grid[r][c] = 0
*count++
for _, d := range dirs {
sink(grid, r+d[0], c+d[1], rows, cols, count)
}
}
class Solution {
private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
private int count;
public int maxAreaOfIsland(int[][] grid) {
if (grid.length == 0)
return 0;
int mx = 0;
int rows = grid.length, cols = grid[0].length;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] == 1) {
count = 0;
sink(grid, i, j, rows, cols);
mx = Math.max(count, mx);
}
return mx;
}
private void sink(int[][] grid, int r, int c, int rows, int cols) {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] != 1)
return;
grid[r][c] = 0;
count++;
for (int[] d : DIRS)
sink(grid, r + d[0], c + d[1], rows, cols);
}
}
const DIRS = [
[1, 0],
[0, 1],
[-1, 0],
[0, -1],
];
/**
* @param {number[][]} grid
* @return {number}
*/
var maxAreaOfIsland = function (grid) {
if (grid.length === 0) return 0;
let mx = 0,
count = 0;
const rows = grid.length,
cols = grid[0].length;
const sink = (r, c) => {
if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] !== 1) return;
grid[r][c] = 0;
count++;
for (const [dr, dc] of DIRS) sink(r + dr, c + dc);
};
for (let i = 0; i < rows; i++)
for (let j = 0; j < cols; j++)
if (grid[i][j] === 1) {
count = 0;
sink(i, j);
mx = Math.max(count, mx);
}
return mx;
};
from typing import List
DIRS = ((1, 0), (0, 1), (-1, 0), (0, -1))
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
if not grid:
return 0
mx = 0
rows, cols = len(grid), len(grid[0])
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
mx = max(self.sink(grid, i, j, rows, cols), mx)
return mx
def sink(self, grid: List[List[int]], r: int, c: int, rows: int, cols: int) -> int:
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] != 1:
return 0
grid[r][c] = 0
count = 1
for dr, dc in DIRS:
count += self.sink(grid, r + dr, c + dc, rows, cols)
return count
const DIRS: [(isize, isize); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)];
impl Solution {
pub fn max_area_of_island(mut grid: Vec<Vec<i32>>) -> i32 {
if grid.is_empty() {
return 0;
}
let mut mx = 0;
let (rows, cols) = (grid.len(), grid[0].len());
for i in 0..rows {
for j in 0..cols {
if grid[i][j] == 1 {
let mut count = 0;
Self::sink(&mut grid, i, j, rows, cols, &mut count);
mx = mx.max(count);
}
}
}
mx
}
fn sink(grid: &mut Vec<Vec<i32>>, r: usize, c: usize, rows: usize, cols: usize, count: &mut i32) {
if grid[r][c] != 1 {
return;
}
grid[r][c] = 0;
*count += 1;
for (dr, dc) in DIRS {
// wrapping_add_signed turns -1 at 0 into usize::MAX, caught by the bounds check
let (nr, nc) = (r.wrapping_add_signed(dr), c.wrapping_add_signed(dc));
if nr < rows && nc < cols {
Self::sink(grid, nr, nc, rows, cols, count);
}
}
}
}