Skip to content

Commit 8ab6773

Browse files
committed
feat: update js solution to lc problem: No.1568
1 parent b9226e3 commit 8ab6773

File tree

3 files changed

+80
-116
lines changed

3 files changed

+80
-116
lines changed

solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -361,54 +361,42 @@ function minDays(grid: number[][]): number {
361361
* @return {number}
362362
*/
363363
var minDays = function (grid) {
364-
const directions = [
365-
[0, 1],
366-
[1, 0],
367-
[0, -1],
368-
[-1, 0],
369-
];
370-
const rows = grid.length;
371-
const cols = grid[0].length;
372-
373-
function dfs(x, y, visited) {
374-
visited[x][y] = true;
375-
for (let [dx, dy] of directions) {
376-
const nx = x + dx,
377-
ny = y + dy;
378-
if (
379-
nx >= 0 &&
380-
ny >= 0 &&
381-
nx < rows &&
382-
ny < cols &&
383-
grid[nx][ny] === 1 &&
384-
!visited[nx][ny]
385-
) {
386-
dfs(nx, ny, visited);
387-
}
364+
const dirs = [-1, 0, 1, 0, -1];
365+
const [m, n] = [grid.length, grid[0].length];
366+
367+
const dfs = (i, j, visited) => {
368+
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
369+
return;
388370
}
389-
}
390371

391-
function countIslands() {
392-
let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
393-
let count = 0;
394-
for (let i = 0; i < rows; i++) {
395-
for (let j = 0; j < cols; j++) {
396-
if (grid[i][j] === 1 && !visited[i][j]) {
397-
count++;
398-
dfs(i, j, visited);
372+
visited[i][j] = true;
373+
for (let d = 0; d < 4; d++) {
374+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
375+
dfs(y, x, visited);
376+
}
377+
};
378+
379+
const count = () => {
380+
const vis = Array.from({ length: m }, () => Array(n).fill(false));
381+
let c = 0;
382+
for (let i = 0; i < m; i++) {
383+
for (let j = 0; j < n; j++) {
384+
if (grid[i][j] === 1 && !vis[i][j]) {
385+
c++;
386+
dfs(i, j, vis);
399387
}
400388
}
401389
}
402-
return count;
403-
}
390+
return c;
391+
};
404392

405-
if (countIslands() !== 1) return 0;
393+
if (count() !== 1) return 0;
406394

407-
for (let i = 0; i < rows; i++) {
408-
for (let j = 0; j < cols; j++) {
395+
for (let i = 0; i < m; i++) {
396+
for (let j = 0; j < n; j++) {
409397
if (grid[i][j] === 1) {
410398
grid[i][j] = 0;
411-
if (countIslands() !== 1) return 1;
399+
if (count() !== 1) return 1;
412400
grid[i][j] = 1;
413401
}
414402
}

solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/README_EN.md

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -352,54 +352,42 @@ function minDays(grid: number[][]): number {
352352
* @return {number}
353353
*/
354354
var minDays = function (grid) {
355-
const directions = [
356-
[0, 1],
357-
[1, 0],
358-
[0, -1],
359-
[-1, 0],
360-
];
361-
const rows = grid.length;
362-
const cols = grid[0].length;
363-
364-
function dfs(x, y, visited) {
365-
visited[x][y] = true;
366-
for (let [dx, dy] of directions) {
367-
const nx = x + dx,
368-
ny = y + dy;
369-
if (
370-
nx >= 0 &&
371-
ny >= 0 &&
372-
nx < rows &&
373-
ny < cols &&
374-
grid[nx][ny] === 1 &&
375-
!visited[nx][ny]
376-
) {
377-
dfs(nx, ny, visited);
378-
}
355+
const dirs = [-1, 0, 1, 0, -1];
356+
const [m, n] = [grid.length, grid[0].length];
357+
358+
const dfs = (i, j, visited) => {
359+
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
360+
return;
379361
}
380-
}
381362

382-
function countIslands() {
383-
let visited = Array.from({ length: rows }, () => Array(cols).fill(false));
384-
let count = 0;
385-
for (let i = 0; i < rows; i++) {
386-
for (let j = 0; j < cols; j++) {
387-
if (grid[i][j] === 1 && !visited[i][j]) {
388-
count++;
389-
dfs(i, j, visited);
363+
visited[i][j] = true;
364+
for (let d = 0; d < 4; d++) {
365+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
366+
dfs(y, x, visited);
367+
}
368+
};
369+
370+
const count = () => {
371+
const vis = Array.from({ length: m }, () => Array(n).fill(false));
372+
let c = 0;
373+
for (let i = 0; i < m; i++) {
374+
for (let j = 0; j < n; j++) {
375+
if (grid[i][j] === 1 && !vis[i][j]) {
376+
c++;
377+
dfs(i, j, vis);
390378
}
391379
}
392380
}
393-
return count;
394-
}
381+
return c;
382+
};
395383

396-
if (countIslands() !== 1) return 0;
384+
if (count() !== 1) return 0;
397385

398-
for (let i = 0; i < rows; i++) {
399-
for (let j = 0; j < cols; j++) {
386+
for (let i = 0; i < m; i++) {
387+
for (let j = 0; j < n; j++) {
400388
if (grid[i][j] === 1) {
401389
grid[i][j] = 0;
402-
if (countIslands() !== 1) return 1;
390+
if (count() !== 1) return 1;
403391
grid[i][j] = 1;
404392
}
405393
}

solution/1500-1599/1568.Minimum Number of Days to Disconnect Island/Solution.js

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,42 @@
33
* @return {number}
44
*/
55
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;
6+
const dirs = [-1, 0, 1, 0, -1];
7+
const [m, n] = [grid.length, grid[0].length];
148

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-
}
9+
const dfs = (i, j, visited) => {
10+
if (i < 0 || m <= i || j < 0 || n <= j || grid[i][j] === 0 || visited[i][j]) {
11+
return;
3012
}
31-
}
3213

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);
14+
visited[i][j] = true;
15+
for (let d = 0; d < 4; d++) {
16+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
17+
dfs(y, x, visited);
18+
}
19+
};
20+
21+
const count = () => {
22+
const vis = Array.from({ length: m }, () => Array(n).fill(false));
23+
let c = 0;
24+
for (let i = 0; i < m; i++) {
25+
for (let j = 0; j < n; j++) {
26+
if (grid[i][j] === 1 && !vis[i][j]) {
27+
c++;
28+
dfs(i, j, vis);
4129
}
4230
}
4331
}
44-
return count;
45-
}
32+
return c;
33+
};
4634

47-
if (countIslands() !== 1) return 0;
35+
if (count() !== 1) return 0;
4836

49-
for (let i = 0; i < rows; i++) {
50-
for (let j = 0; j < cols; j++) {
37+
for (let i = 0; i < m; i++) {
38+
for (let j = 0; j < n; j++) {
5139
if (grid[i][j] === 1) {
5240
grid[i][j] = 0;
53-
if (countIslands() !== 1) return 1;
41+
if (count() !== 1) return 1;
5442
grid[i][j] = 1;
5543
}
5644
}

0 commit comments

Comments
 (0)