-
Notifications
You must be signed in to change notification settings - Fork 0
/
1568. Minimum number of days to disconnect island
53 lines (43 loc) · 1.44 KB
/
1568. Minimum number of days to disconnect island
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//1568. Minimum number of days to disconnect island
class Solution {
public int minDays(int[][] grid) {
if(countIslands(grid) != 1) {
return 0;
}
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] == 1) {
grid[i][j] = 0;
if(countIslands(grid) != 1) {
return 1;
}
grid[i][j] = 1;
}
}
}
return 2;
}
private int countIslands(int[][] grid) {
boolean[][] seen = new boolean[grid.length][grid[0].length];
int islands = 0;
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
if(grid[i][j] == 1 && !seen[i][j]) {
islands++;
dfs(grid, i, j, seen);
}
}
}
return islands;
}
private void dfs(int[][] grid, int r, int c, boolean[][] seen) {
seen[r][c] = true;
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for(int[] dir : directions) {
int nr = r + dir[0], nc = c + dir[1];
if(nr >= 0 && nr < grid.length && nc >= 0 && nc < grid[0].length && grid[nr][nc] == 1 && !seen[nr][nc]) {
dfs(grid, nr, nc, seen);
}
}
}
}