File tree 3 files changed +190
-0
lines changed
solution/1500-1599/1568.Minimum Number of Days to Disconnect Island
3 files changed +190
-0
lines changed Original file line number Diff line number Diff line change @@ -295,6 +295,71 @@ func minDays(grid [][]int) int {
295
295
}
296
296
```
297
297
298
+ #### JavaScript
299
+
300
+ ``` js
301
+ /**
302
+ * @param {number[][]} grid
303
+ * @return {number}
304
+ */
305
+ var minDays = function (grid ) {
306
+ const directions = [
307
+ [0 , 1 ],
308
+ [1 , 0 ],
309
+ [0 , - 1 ],
310
+ [- 1 , 0 ],
311
+ ];
312
+ const rows = grid .length ;
313
+ const cols = grid[0 ].length ;
314
+
315
+ function dfs (x , y , visited ) {
316
+ visited[x][y] = true ;
317
+ for (let [dx, dy] of directions) {
318
+ const nx = x + dx,
319
+ ny = y + dy;
320
+ if (
321
+ nx >= 0 &&
322
+ ny >= 0 &&
323
+ nx < rows &&
324
+ ny < cols &&
325
+ grid[nx][ny] === 1 &&
326
+ ! visited[nx][ny]
327
+ ) {
328
+ dfs (nx, ny, visited);
329
+ }
330
+ }
331
+ }
332
+
333
+ function countIslands () {
334
+ let visited = Array .from ({ length: rows }, () => Array (cols).fill (false ));
335
+ let count = 0 ;
336
+ for (let i = 0 ; i < rows; i++ ) {
337
+ for (let j = 0 ; j < cols; j++ ) {
338
+ if (grid[i][j] === 1 && ! visited[i][j]) {
339
+ count++ ;
340
+ dfs (i, j, visited);
341
+ }
342
+ }
343
+ }
344
+ return count;
345
+ }
346
+
347
+ if (countIslands () !== 1 ) return 0 ;
348
+
349
+ for (let i = 0 ; i < rows; i++ ) {
350
+ for (let j = 0 ; j < cols; j++ ) {
351
+ if (grid[i][j] === 1 ) {
352
+ grid[i][j] = 0 ;
353
+ if (countIslands () !== 1 ) return 1 ;
354
+ grid[i][j] = 1 ;
355
+ }
356
+ }
357
+ }
358
+
359
+ return 2 ;
360
+ };
361
+ ```
362
+
298
363
<!-- tabs:end -->
299
364
300
365
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -286,6 +286,71 @@ func minDays(grid [][]int) int {
286
286
}
287
287
```
288
288
289
+ #### JavaScript
290
+
291
+ ``` js
292
+ /**
293
+ * @param {number[][]} grid
294
+ * @return {number}
295
+ */
296
+ var minDays = function (grid ) {
297
+ const directions = [
298
+ [0 , 1 ],
299
+ [1 , 0 ],
300
+ [0 , - 1 ],
301
+ [- 1 , 0 ],
302
+ ];
303
+ const rows = grid .length ;
304
+ const cols = grid[0 ].length ;
305
+
306
+ function dfs (x , y , visited ) {
307
+ visited[x][y] = true ;
308
+ for (let [dx, dy] of directions) {
309
+ const nx = x + dx,
310
+ ny = y + dy;
311
+ if (
312
+ nx >= 0 &&
313
+ ny >= 0 &&
314
+ nx < rows &&
315
+ ny < cols &&
316
+ grid[nx][ny] === 1 &&
317
+ ! visited[nx][ny]
318
+ ) {
319
+ dfs (nx, ny, visited);
320
+ }
321
+ }
322
+ }
323
+
324
+ function countIslands () {
325
+ let visited = Array .from ({ length: rows }, () => Array (cols).fill (false ));
326
+ let count = 0 ;
327
+ for (let i = 0 ; i < rows; i++ ) {
328
+ for (let j = 0 ; j < cols; j++ ) {
329
+ if (grid[i][j] === 1 && ! visited[i][j]) {
330
+ count++ ;
331
+ dfs (i, j, visited);
332
+ }
333
+ }
334
+ }
335
+ return count;
336
+ }
337
+
338
+ if (countIslands () !== 1 ) return 0 ;
339
+
340
+ for (let i = 0 ; i < rows; i++ ) {
341
+ for (let j = 0 ; j < cols; j++ ) {
342
+ if (grid[i][j] === 1 ) {
343
+ grid[i][j] = 0 ;
344
+ if (countIslands () !== 1 ) return 1 ;
345
+ grid[i][j] = 1 ;
346
+ }
347
+ }
348
+ }
349
+
350
+ return 2 ;
351
+ };
352
+ ```
353
+
289
354
<!-- tabs:end -->
290
355
291
356
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } grid
3
+ * @return {number }
4
+ */
5
+ var minDays = function ( grid ) {
6
+ const directions = [
7
+ [ 0 , 1 ] ,
8
+ [ 1 , 0 ] ,
9
+ [ 0 , - 1 ] ,
10
+ [ - 1 , 0 ] ,
11
+ ] ;
12
+ const rows = grid . length ;
13
+ const cols = grid [ 0 ] . length ;
14
+
15
+ function dfs ( x , y , visited ) {
16
+ visited [ x ] [ y ] = true ;
17
+ for ( let [ dx , dy ] of directions ) {
18
+ const nx = x + dx ,
19
+ ny = y + dy ;
20
+ if (
21
+ nx >= 0 &&
22
+ ny >= 0 &&
23
+ nx < rows &&
24
+ ny < cols &&
25
+ grid [ nx ] [ ny ] === 1 &&
26
+ ! visited [ nx ] [ ny ]
27
+ ) {
28
+ dfs ( nx , ny , visited ) ;
29
+ }
30
+ }
31
+ }
32
+
33
+ function countIslands ( ) {
34
+ let visited = Array . from ( { length : rows } , ( ) => Array ( cols ) . fill ( false ) ) ;
35
+ let count = 0 ;
36
+ for ( let i = 0 ; i < rows ; i ++ ) {
37
+ for ( let j = 0 ; j < cols ; j ++ ) {
38
+ if ( grid [ i ] [ j ] === 1 && ! visited [ i ] [ j ] ) {
39
+ count ++ ;
40
+ dfs ( i , j , visited ) ;
41
+ }
42
+ }
43
+ }
44
+ return count ;
45
+ }
46
+
47
+ if ( countIslands ( ) !== 1 ) return 0 ;
48
+
49
+ for ( let i = 0 ; i < rows ; i ++ ) {
50
+ for ( let j = 0 ; j < cols ; j ++ ) {
51
+ if ( grid [ i ] [ j ] === 1 ) {
52
+ grid [ i ] [ j ] = 0 ;
53
+ if ( countIslands ( ) !== 1 ) return 1 ;
54
+ grid [ i ] [ j ] = 1 ;
55
+ }
56
+ }
57
+ }
58
+
59
+ return 2 ;
60
+ } ;
You can’t perform that action at this time.
0 commit comments