Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4abf58d

Browse files
Add 695 in c language
1 parent825a25b commit4abf58d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎c/695-Max-Area-of-Island.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Return the maximum area of an island in grid. If there is no island, return 0.
3+
Time: O(n^2)
4+
Space: O(1)
5+
6+
*/
7+
8+
intmax(inta,intb) {
9+
returna>b?a:b;
10+
}
11+
12+
intdfs(int**grid,intn,intm,inti,intj) {
13+
intcpt=1;
14+
grid[i][j]=0;
15+
if (i>0&&grid[i-1][j]==1)
16+
cpt+=dfs(grid,n,m,i-1,j);
17+
if (j>0&&grid[i][j-1]==1)
18+
cpt+=dfs(grid,n,m,i,j-1);
19+
if (i<(n-1)&&grid[i+1][j]==1)
20+
cpt+=dfs(grid,n,m,i+1,j);
21+
if (j<(m-1)&&grid[i][j+1]==1)
22+
cpt+=dfs(grid,n,m,i,j+1);
23+
returncpt;
24+
}
25+
26+
intmaxAreaOfIsland(int**grid,intgridSize,int*gridColSize){
27+
intm=0,h=*gridColSize,i,j;
28+
for (i=0;i<gridSize;i++) {
29+
for (j=0;j<h;j++) {
30+
if (grid[i][j]==1)
31+
m=max(m,dfs(grid,gridSize,h,i,j));
32+
}
33+
}
34+
returnm;
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp