forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfEnclaves.java
More file actions
52 lines (42 loc) · 1.4 KB
/
NumberOfEnclaves.java
File metadata and controls
52 lines (42 loc) · 1.4 KB
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
package graph;
/**
* Description: https://leetcode.com/problems/number-of-enclaves
* Difficulty: Medium
* Time complexity: O(m * n)
* Space complexity: O(m * n)
*/
public class NumberOfEnclaves {
private final int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int numEnclaves(int[][] grid) {
int[][] visited = new int[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
dfs(grid, visited, i, 0);
dfs(grid, visited, i, grid[0].length - 1);
}
for (int i = 0; i < grid[0].length; i++) {
dfs(grid, visited, 0, i);
dfs(grid, visited, grid.length - 1, i);
}
int count = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1 && visited[i][j] == 0) {
count++;
}
}
}
return count;
}
private void dfs(int[][] grid, int[][] visited, int x, int y) {
if (grid[x][y] == 0) return;
visited[x][y] = 1;
for (int[] dir : directions) {
int x1 = x + dir[0];
int y1 = y + dir[1];
if (x1 >= 0 && x1 < grid.length && y1 >= 0 && y1 < grid[0].length
&& visited[x1][y1] == 0) {
dfs(grid, visited, x1, y1);
}
}
}
}