-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfEnclaves.java
193 lines (164 loc) · 5.47 KB
/
NumberOfEnclaves.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*https://leetcode.com/problems/number-of-enclaves/*/
/*https://binarysearch.com/problems/Enclosed-Islands*/
//simple dfs solution
class Solution {
boolean[][] visited;
int coveredOnes, m, n;
int[][] d = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
public int numEnclaves(int[][] grid) {
int i, j, totalOnes = 0;
m = grid.length;
n = grid[0].length;
visited = new boolean[m][n];
coveredOnes = 0;
//count the number of total 1s
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
if (grid[i][j] == 1) ++totalOnes;
//count the number of 1s those can be covered from each boundary
for (i = 0; i < m; ++i) //left boundary
{
if (grid[i][0] == 1 && !visited[i][0]) //if current cell is 1 and unvisited
dfs(grid, i, 0);
}
for (j = 0; j < n; ++j) //top boundary
{
if (grid[0][j] == 1 && !visited[0][j]) //if current cell is 1 and unvisited
dfs(grid, 0, j);
}
for (i = 0; i < m; ++i) //right boundary
{
if (grid[i][n-1] == 1 && !visited[i][n-1]) //if current cell is 1 and unvisited
dfs(grid, i, n-1);
}
for (j = 0; j < n; ++j) //bottom boundary
{
if (grid[m-1][j] == 1 && !visited[m-1][j]) //if current cell is 1 and unvisited
dfs(grid, m-1, j);
}
return totalOnes-coveredOnes;
}
public void dfs(int[][] grid, int r, int c)
{
visited[r][c] = true; //mark as visited
++coveredOnes; //count covered ones
for (int i = 0; i < 4; ++i) //for each direction
if (isFeasible(grid,r+d[i][0],c+d[i][1])) //if feasible
dfs(grid, r+d[i][0], c+d[i][1]); //recur
}
public boolean isFeasible(int[][] grid, int r, int c)
{
if(r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == 1 && !visited[r][c]) return true;
return false;
}
}
//above solution optimized version
class Solution {
boolean[][] visited;
int coveredOnes, m, n;
int[][] d = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};
public int numEnclaves(int[][] grid) {
int i, j, totalOnes = 0;
m = grid.length;
n = grid[0].length;
visited = new boolean[m][n];
coveredOnes = 0;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
totalOnes += grid[i][j];
for (i = 0; i < m; ++i)
{
dfs(grid, i, 0); //left boundary
dfs(grid, i, n-1); //right boundary
}
for (j = 0; j < n; ++j)
{
dfs(grid, 0, j); //top boundary
dfs(grid, m-1, j); //bottom boundary
}
return totalOnes-coveredOnes;
}
public void dfs(int[][] grid, int r, int c)
{
if (!isFeasible(grid, r, c)) return;
visited[r][c] = true;
++coveredOnes;
for (int i = 0; i < 4; ++i)
dfs(grid, r+d[i][0], c+d[i][1]);
}
public boolean isFeasible(int[][] grid, int r, int c)
{
if(r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == 1 && !visited[r][c]) return true;
return false;
}
}
//another efficient solution in reverse direction
class Solution {
public int numEnclaves(int[][] grid) {
//flood for each boundary and modify all reachable 1s to 0s
for (int y = 0; y < grid.length; y++) {
flood(grid, y, 0); //left boundary
flood(grid, y, grid[y].length - 1); //right boundary
}
for (int x = 0; x < grid[0].length; x++) {
flood(grid, 0, x); //top boundary
flood(grid, grid.length - 1, x); //bottom boundary
}
//count the number of 1s and return
int res = 0;
for (int y = 1; y < grid.length - 1; y++) {
for (int x = 1; x < grid[y].length - 1; x++) {
res += grid[y][x];
}
}
return res;
}
private static void flood(int[][] grid, int y, int x) {
//if infeasible, return
if (y < 0 || y >= grid.length || x < 0 || x >= grid[y].length || grid[y][x] != 1) {
return;
}
grid[y][x] = 0; //set 0
//flood for each direction
flood(grid, y - 1, x);
flood(grid, y + 1, x);
flood(grid, y, x - 1);
flood(grid, y, x + 1);
}
}
import java.util.*;
class Solution {
int m, n;
int[][] pos = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
public int solve(int[][] matrix) {
int i, j;
m = matrix.length; n = matrix[0].length;
for (i = 0; i < m; ++i)
{
if (matrix[i][0] == 1) dfs(matrix,i,0);
if (matrix[i][n-1] == 1) dfs(matrix,i,n-1);
}
for (j = 0; j < n; ++j)
{
if (matrix[0][j] == 1) dfs(matrix,0,j);
if (matrix[m-1][j] == 1) dfs(matrix,m-1,j);
}
int result = 0;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
result += matrix[i][j];
return result;
}
public void dfs(int[][] matrix, int row, int col)
{
matrix[row][col] = 0;
int r, c, i;
for (i = 0; i < 4; ++i)
{
r = row+pos[i][0];
c = col+pos[i][1];
if (r >= 0 && r < m && c >= 0 && c < n && matrix[r][c] == 1)
dfs(matrix,r,c);
}
}
}