forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumIslands.java
34 lines (27 loc) · 978 Bytes
/
NumIslands.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//O(M*N) runtime where M is rows, N is columns
//O(M*N) grid maps is filled with space
// We do iterate across the MN items in the grid, but for each element we don't
// actually visit MN items. Each cell has at the worst case 4 neighbors. So overall we
// visit at most O(4*M*N) cells (which is still O(MN)).
public int numIslands(char[][] grid) {
int res = 0;
for(int i=0; i<grid.length; i++){
for(int j=0; j<grid[0].length; j++){
if(grid[i][j] == '1'){
res += dfsSink(grid, i, j);
}
}
}
return res;
}
public int dfsSink(char[][] grid, int i, int j){
if(i<0 || i>=grid.length || j<0 || j>=grid[0].length || grid[i][j] == '0'){
return 0;
}
grid[i][j] = '0';
dfsSink(grid, i+1, j);
dfsSink(grid, i-1, j);
dfsSink(grid, i, j+1);
dfsSink(grid, i, j-1);
return 1;
}