Open azl397985856 opened 6 months ago
var maxAreaOfIsland = function (grid) {
let x = grid[0].length,
y = grid.length;
let res = 0;
function dfs(i, j) {
if (i >= y || j >= x || j < 0 || i < 0 || grid[i][j] != 1) return 0;
grid[i][j] = -1;
return 1 + dfs(i + 1, j) + dfs(i - 1, j) + dfs(i, j - 1) + dfs(i, j + 1);
}
for (let i = 0; i < y; i++) {
for (let j = 0; j < x; j++) {
if (grid[i][j]) {
res = Math.max(res, dfs(i, j));
}
}
}
return res;
};
时间复杂度:O(mn) 空间复杂度:O(mn)
class Solution {
int maxAreaOfIsland(int[][] grid) {
int res = 0;
int m = grid.length, n = grid[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
res = Math.max(res, dfs(grid, i, j));
}
}
}
return res;
}
int dfs(int[][] grid, int i, int j) {
int m = grid.length, n = grid[0].length;
if (i < 0 || j < 0 || i >= m || j >= n) {
return 0;
}
if (grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
return dfs(grid, i + 1, j)
+ dfs(grid, i, j + 1)
+ dfs(grid, i - 1, j)
+ dfs(grid, i, j - 1) + 1;
}
}
function maxAreaOfIsland(grid: number[][]): number {
const row:number = grid.length,
col:number = grid[0].length;
let maxIsland:number = 0;
interface IDfs {
(i:number, j:number):number
}
const dfs:IDfs = (i, j) => {
// 边界判断
let iJudge:boolean = i < 0 || i >= row,
jJudge:boolean = j < 0 || j >= col;
if (iJudge || jJudge || grid[i][j] === 0) {
// 不符合条件则 直接返回0
return 0;
}
// 遍历过的节点置 0 防止多次计数
grid[i][j] = 0;
let cntIsland:number = 1;
cntIsland += dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1);
return cntIsland;
}
// 遍历地图(因遍历过后的节点都为0,不会重复遍历),并取最大值
for(let i:number = 0; i < row; i++) {
for (let j:number = 0; j < col; j++) {
maxIsland = Math.max(maxIsland, dfs(i,j));
}
}
return maxIsland;
};
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
if not grid:
return 0
def dfs(x, y):
if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]) or grid[x][y] == 0:
return 0
grid[x][y] = 0 # Mark as visited by setting to 0
return (1 +
dfs(x+1, y) +
dfs(x-1, y) +
dfs(x, y+1) +
dfs(x, y-1))
max_area = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
max_area = max(max_area, dfs(i, j))
return max_area
python3代码
class Solution:
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
m,n = len(grid),len(grid[0])
def dfs(x,y):
if x<0 or x>=m or y<0 or y>=n or grid[x][y]!=1:
return 0
grid[x][y]=-1
area=1
area+=dfs(x+1,y)
area+=dfs(x-1,y)
area+=dfs(x,y-1)
area+=dfs(x,y+1)
return area
rs=0
for i in range(m):
for j in range(n):
if grid[i][j]==1:
rs=max(rs,dfs(i,j))
return rs
class Solution {
public:
typedef pair<int, int> PII;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int bfs(vector<vector<int>>& grid, vector<vector<bool>>& st, int x, int y) {
int area = 1;
queue<PII> q;
q.push({x, y});
while (!q.empty()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int xi = t.first + dx[i];
int yi = t.second + dy[i];
if (xi < 0 || xi >= grid.size() || yi < 0 ||
yi >= grid[0].size()) {
continue;
}
if (!st[xi][yi] && grid[xi][yi] == 1) {
area++;
st[xi][yi] = true;
q.push({xi, yi});
}
}
}
return area;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
vector<vector<bool>> st =
vector<vector<bool>>(n, vector<bool>(m, false));
int cnt = 0;
int max_area = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!st[i][j] && grid[i][j] == 1) {
st[i][j] = true;
cnt++;
int area = bfs(grid, st, i, j);
max_area = max(max_area, area);
}
}
}
if (cnt == 0)
return 0;
return max_area;
}
};
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
m,n = len(grid), len(grid[0])
ans = 0
def dfs(i,j):
if i < 0 or i >= m or j < 0 or j >= n: return 0
if grid[i][j] == 0: return 0
grid[i][j] = 0
top = dfs(i+1,j)
bottom = dfs(i-1,j)
left = dfs(i,j-1)
right = dfs(i,j+1)
return 1 + sum([top, bottom,left, right])
for i in range(m):
for j in range(n):
ans = max(ans, dfs(i,j))
return ans
695. 岛屿的最大面积
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/max-area-of-island/
前置知识
暂无
题目描述