comments | edit_url |
---|---|
true |
给定一个由 0
和 1
组成的非空二维数组 grid
,用来表示海洋岛屿地图。
一个 岛屿 是由一些相邻的 1
(代表土地) 构成的组合,这里的「相邻」要求两个 1
必须在水平或者竖直方向上相邻。你可以假设 grid
的四个边缘都被 0
(代表水)包围着。
找到给定的二维数组中最大的岛屿面积。如果没有岛屿,则返回面积为 0
。
示例 1:
输入: 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]] 输出: 6 解释: 对于上面这个给定矩阵应返回6
。注意答案不应该是11
,因为岛屿只能包含水平或垂直的四个方向的1
。
示例 2:
输入: grid = [[0,0,0,0,0,0,0,0]] 输出: 0
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 50
grid[i][j] is either 0 or 1
注意:本题与主站 695 题相同: https://leetcode.cn/problems/max-area-of-island/
我们可以遍历每一个格子
时间复杂度
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
def dfs(i: int, j: int) -> int:
if grid[i][j] == 0:
return 0
ans = 1
grid[i][j] = 0
dirs = (-1, 0, 1, 0, -1)
for a, b in pairwise(dirs):
x, y = i + a, j + b
if 0 <= x < m and 0 <= y < n:
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))
class Solution {
private int m;
private int n;
private int[][] grid;
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) {
ans = Math.max(ans, dfs(i, j));
}
}
return ans;
}
private int dfs(int i, int j) {
if (grid[i][j] == 0) {
return 0;
}
int ans = 1;
grid[i][j] = 0;
int[] dirs = {-1, 0, 1, 0, -1};
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
ans += dfs(x, y);
}
}
return ans;
}
}
class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
int dirs[5] = {-1, 0, 1, 0, -1};
int ans = 0;
function<int(int, int)> dfs = [&](int i, int j) {
if (grid[i][j] == 0) {
return 0;
}
int ans = 1;
grid[i][j] = 0;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n) {
ans += dfs(x, y);
}
}
return ans;
};
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans = max(ans, dfs(i, j));
}
}
return ans;
}
};
func maxAreaOfIsland(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
dirs := [5]int{-1, 0, 1, 0, -1}
var dfs func(i, j int) int
dfs = func(i, j int) int {
if grid[i][j] == 0 {
return 0
}
ans := 1
grid[i][j] = 0
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n {
ans += dfs(x, y)
}
}
return ans
}
for i := range grid {
for j := range grid[i] {
ans = max(ans, dfs(i, j))
}
}
return
}
function maxAreaOfIsland(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i: number, j: number): number => {
if (grid[i][j] === 0) {
return 0;
}
let ans = 1;
grid[i][j] = 0;
for (let k = 0; k < 4; ++k) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n) {
ans += dfs(x, y);
}
}
return ans;
};
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
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 {
private var m = 0
private var n = 0
private var grid: [[Int]] = []
func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
self.m = grid.count
self.n = grid[0].count
self.grid = grid
var maxArea = 0
for i in 0..<m {
for j in 0..<n {
maxArea = max(maxArea, dfs(i, j))
}
}
return maxArea
}
private func dfs(_ i: Int, _ j: Int) -> Int {
if grid[i][j] == 0 {
return 0
}
var area = 1
grid[i][j] = 0
let dirs = [-1, 0, 1, 0, -1]
for k in 0..<4 {
let x = i + dirs[k], y = j + dirs[k + 1]
if x >= 0 && x < m && y >= 0 && y < n {
area += dfs(x, y)
}
}
return area
}
}