Skip to content

Commit 7b96117

Browse files
committed
feat: add solutions to lc/lcof2 problem: Max Area of Island
1 parent 436e258 commit 7b96117

File tree

9 files changed

+1122
-0
lines changed

9 files changed

+1122
-0
lines changed

lcof2/剑指 Offer II 105. 岛屿的最大面积/README.md

+393
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int maxAreaOfIsland(vector<vector<int>>& grid) {
4+
int m = grid.size(), n = grid[0].size();
5+
int res = 0;
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
int t = dfs(grid, i, j, m, n);
9+
res = max(res, t);
10+
}
11+
}
12+
return res;
13+
}
14+
private:
15+
vector<vector<int>> directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
16+
17+
int dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
18+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
19+
return 0;
20+
}
21+
grid[i][j] = 0;
22+
int res = 1;
23+
for (auto direction : directions) {
24+
res += dfs(grid, i + direction[0], j + direction[1], m, n);
25+
}
26+
return res;
27+
}
28+
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var p []int
2+
3+
func maxAreaOfIsland(grid [][]int) int {
4+
m, n := len(grid), len(grid[0])
5+
p = make([]int, m*n)
6+
size := make([]int, m*n)
7+
for i := 0; i < m; i++ {
8+
for j := 0; j < n; j++ {
9+
p[i*n+j] = i*n + j
10+
size[i*n+j] = 1
11+
}
12+
}
13+
for i := 0; i < m; i++ {
14+
for j := 0; j < n; j++ {
15+
if grid[i][j] == 1 {
16+
if i < m-1 && grid[i+1][j] == 1 {
17+
a, b := find(i*n+j), find((i+1)*n+j)
18+
if a != b {
19+
size[a] += size[b]
20+
}
21+
p[b] = a
22+
}
23+
if j < n-1 && grid[i][j+1] == 1 {
24+
a, b := find(i*n+j), find(i*n+j+1)
25+
if a != b {
26+
size[a] += size[b]
27+
}
28+
p[b] = a
29+
}
30+
}
31+
}
32+
}
33+
res := 0
34+
for i := 0; i < m; i++ {
35+
for j := 0; j < n; j++ {
36+
if grid[i][j] == 1 {
37+
res = max(res, size[i*n+j])
38+
}
39+
}
40+
}
41+
return res
42+
}
43+
44+
func find(x int) int {
45+
if p[x] != x {
46+
p[x] = find(p[x])
47+
}
48+
return p[x]
49+
}
50+
51+
func max(a, b int) int {
52+
if a > b {
53+
return a
54+
}
55+
return b
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
private int[][] directions = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}};
3+
4+
public int maxAreaOfIsland(int[][] grid) {
5+
int m = grid.length, n = grid[0].length;
6+
int res = 0;
7+
for (int i = 0; i < m; ++i) {
8+
for (int j = 0; j < n; ++j) {
9+
int t = dfs(grid, i, j, m, n);
10+
res = Math.max(res, t);
11+
}
12+
}
13+
return res;
14+
}
15+
16+
private int dfs(int[][] grid, int i, int j, int m, int n) {
17+
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) {
18+
return 0;
19+
}
20+
grid[i][j] = 0;
21+
int res = 1;
22+
for (int[] direction : directions) {
23+
res += dfs(grid, i + direction[0], j + direction[1], m, n);
24+
}
25+
return res;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
3+
def dfs(grid, i, j, m, n):
4+
if i < 0 or i >= m or j < 0 or j >= n or grid[i][j] == 0:
5+
return 0
6+
grid[i][j] = 0
7+
res = 1
8+
for x, y in [[0, 1], [0, -1], [1, 0], [-1, 0]]:
9+
res += dfs(grid, i + x, j + y, m, n)
10+
return res
11+
12+
m, n = len(grid), len(grid[0])
13+
res = 0
14+
for i in range(m):
15+
for j in range(n):
16+
t = dfs(grid, i, j, m, n)
17+
res = max(res, t)
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function maxAreaOfIsland(grid: number[][]): number {
2+
let m = grid.length, n = grid[0].length;
3+
let res = 0;
4+
for (let i = 0; i < m; ++i) {
5+
for (let j = 0; j < n; ++j) {
6+
if (grid[i][j] == 1) {
7+
res = Math.max(dfs(grid, i, j), res);
8+
}
9+
}
10+
}
11+
return res;
12+
};
13+
14+
function dfs(grid: number[][], i: number, j: number): number {
15+
let m = grid.length, n = grid[0].length;
16+
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || grid[i][j] == 0) {
17+
return 0;
18+
}
19+
grid[i][j] = 0;
20+
let res = 1;
21+
for (let [dx, dy] of [[0, 1], [0, -1], [1, 0], [-1, 0]]) {
22+
res += dfs(grid, i + dx, j + dy);
23+
}
24+
return res;
25+
}

0 commit comments

Comments
 (0)