-
Notifications
You must be signed in to change notification settings - Fork 43
/
NumberOfIslands.java
253 lines (204 loc) · 7.05 KB
/
NumberOfIslands.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package LeetCodeJava.DFS;
// https://leetcode.com/problems/number-of-islands/
import java.util.*;
public class NumberOfIslands {
// V0
// IDEA : DFS
int num_island = 0;
boolean[][] _seen;
public int numIslands(char[][] grid) {
if (grid.length == 1 && grid[0].length == 1){
if (grid[0][0] == '1'){
return 1;
}
return 0;
}
int len = grid.length;
int width = grid[0].length;
// NOTE !!! how we init M X N boolean matrix
this._seen = new boolean[len][width];
for (int i = 0; i < len; i++){
for (int j = 0; j < width; j++){
if (_is_island(grid, j, i, this._seen)){
this.num_island += 1;
}
}
}
return this.num_island;
}
private boolean _is_island(char[][] grid, int x, int y, boolean[][] seen){
int len = grid.length;
int width = grid[0].length;
// NOTE !!! boundary condition : x >= width, y >= len
// since index = lenth - 1
if (x < 0 || x >= width || y < 0 || y >= len || this._seen[y][x] == true || grid[y][x] == '0'){
return false;
}
this._seen[y][x] = true;
/** NOTE !!! we do 4 direction traverse in sequence order */
_is_island(grid, x+1, y, seen);
_is_island(grid, x-1, y, seen);
_is_island(grid, x, y+1, seen);
_is_island(grid, x, y-1, seen);
// NOTE !!! if code can arrive here, means there is at least "1 direction" meet "1" value
// -> there is an island
// -> so we return true as we found an island
return true;
}
// V0'
// IDEA: DFS (with looping)
public int numIslands_0(char[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int l = grid.length;
int w = grid[0].length;
int res = 0;
for (int i = 0; i < l; i++) {
for (int j = 0; j < w; j++) {
/**
* NOTE !!!
*
* if grid[i][j] == '1', no need to collect the coordinate (x,y),
* -> just add res with 1,
* -> and call dfs function
*/
if (grid[i][j] == '1') {
res += 1;
dfs(grid, i, j);
}
}
}
return res;
}
/** NOTE !!!
*
* NO NEED to return boolean val on this helper function (dfs),
* since we mark point as "visited" in place with traversing,
* so no response (void) is OK
*/
private void dfs(char[][] grid, int y, int x) {
int l = grid.length;
int w = grid[0].length;
if (y < 0 || y >= l || x < 0 || x >= w || grid[y][x] != '1') {
return;
}
grid[y][x] = '#'; // Mark the cell as visited
int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
for (int[] dir : dirs) {
int newY = y + dir[0];
int newX = x + dir[1];
dfs(grid, newY, newX);
}
}
// V0'
// IDEA : DFS (with looping) (modified by GPT)
int num_island_2 = 0;
boolean[][] _seen_2;
public int numIslands_0_1(char[][] grid) {
if (grid.length == 1 && grid[0].length == 1) {
return grid[0][0] == '1' ? 1 : 0;
}
int len = grid.length;
int width = grid[0].length;
this._seen = new boolean[len][width];
for (int i = 0; i < len; i++) {
for (int j = 0; j < width; j++) {
if (_is_island_2(grid, j, i, this._seen_2)) {
this.num_island_2 += 1;
}
}
}
return this.num_island_2;
}
private boolean _is_island_2(char[][] grid, int x, int y, boolean[][] seen) {
int[][] directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int len = grid.length;
int width = grid[0].length;
if (x < 0 || x >= width || y < 0 || y >= len || this._seen[y][x] || grid[y][x] == '0') {
return false;
}
this._seen_2[y][x] = true;
for (int[] dir : directions) {
int newX = x + dir[0];
int newY = y + dir[1];
_is_island(grid, newX, newY, seen);
}
return true;
}
// V1
// IDEA : DFS
// https://leetcode.com/problems/number-of-islands/editorial/
void dfs_1(char[][] grid, int r, int c) {
int nr = grid.length;
int nc = grid[0].length;
if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
return;
}
grid[r][c] = '0';
dfs_1(grid, r - 1, c);
dfs_1(grid, r + 1, c);
dfs_1(grid, r, c - 1);
dfs_1(grid, r, c + 1);
}
public int numIslands_1(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
dfs_1(grid, r, c);
}
}
}
return num_islands;
}
// V2
// IDEA : BFS
// https://leetcode.com/problems/number-of-islands/editorial/
public int numIslands_2(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
grid[r][c] = '0'; // mark as visited
Queue<Integer> neighbors = new LinkedList<>();
neighbors.add(r * nc + c);
while (!neighbors.isEmpty()) {
int id = neighbors.remove();
int row = id / nc;
int col = id % nc;
if (row - 1 >= 0 && grid[row-1][col] == '1') {
neighbors.add((row-1) * nc + col);
grid[row-1][col] = '0';
}
if (row + 1 < nr && grid[row+1][col] == '1') {
neighbors.add((row+1) * nc + col);
grid[row+1][col] = '0';
}
if (col - 1 >= 0 && grid[row][col-1] == '1') {
neighbors.add(row * nc + col-1);
grid[row][col-1] = '0';
}
if (col + 1 < nc && grid[row][col+1] == '1') {
neighbors.add(row * nc + col+1);
grid[row][col+1] = '0';
}
}
}
}
}
return num_islands;
}
}