Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2062cfe

Browse files
authored
feat: add ts solution to lc problem: No.1568 (doocs#3402)
1 parent ce2a9c2 commit 2062cfe

File tree

4 files changed

+249
-116
lines changed

4 files changed

+249
-116
lines changed

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

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,64 @@ func minDays(grid [][]int) int {
295295
}
296296
```
297297

298+
#### TypeScript
299+
300+
```ts
301+
function minDays(grid: number[][]): number {
302+
const [m, n] = [grid.length, grid[0].length];
303+
304+
const dfs = (i: number, j: number) => {
305+
if (i < 0 || m <= i || j < 0 || n <= j || [0, 2].includes(grid[i][j])) return;
306+
307+
grid[i][j] = 2;
308+
const dir = [-1, 0, 1, 0, -1];
309+
for (let k = 0; k < 4; k++) {
310+
const [y, x] = [i + dir[k], j + dir[k + 1]];
311+
dfs(y, x);
312+
}
313+
};
314+
315+
const count = () => {
316+
let c = 0;
317+
318+
for (let i = 0; i < m; i++) {
319+
for (let j = 0; j < n; j++) {
320+
if (grid[i][j] === 1) {
321+
dfs(i, j);
322+
c++;
323+
}
324+
}
325+
}
326+
327+
for (let i = 0; i < m; i++) {
328+
for (let j = 0; j < n; j++) {
329+
if (grid[i][j] === 2) {
330+
grid[i][j] = 1;
331+
}
332+
}
333+
}
334+
335+
return c;
336+
};
337+
338+
if (count() !== 1) return 0;
339+
340+
for (let i = 0; i < m; i++) {
341+
for (let j = 0; j < n; j++) {
342+
if (grid[i][j] === 1) {
343+
grid[i][j] = 0;
344+
345+
if (count() !== 1) return 1;
346+
347+
grid[i][j] = 1;
348+
}
349+
}
350+
}
351+
352+
return 2;
353+
}
354+
```
355+
298356
#### JavaScript
299357

300358
```js
@@ -303,54 +361,42 @@ func minDays(grid [][]int) int {
303361
* @return {number}
304362
*/
305363
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-
}
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;
330370
}
331-
}
332371

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);
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);
341387
}
342388
}
343389
}
344-
return count;
345-
}
390+
return c;
391+
};
346392

347-
if (countIslands() !== 1) return 0;
393+
if (count() !== 1) return 0;
348394

349-
for (let i = 0; i < rows; i++) {
350-
for (let j = 0; j < cols; j++) {
395+
for (let i = 0; i < m; i++) {
396+
for (let j = 0; j < n; j++) {
351397
if (grid[i][j] === 1) {
352398
grid[i][j] = 0;
353-
if (countIslands() !== 1) return 1;
399+
if (count() !== 1) return 1;
354400
grid[i][j] = 1;
355401
}
356402
}

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

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,64 @@ func minDays(grid [][]int) int {
286286
}
287287
```
288288

289+
#### TypeScript
290+
291+
```ts
292+
function minDays(grid: number[][]): number {
293+
const [m, n] = [grid.length, grid[0].length];
294+
295+
const dfs = (i: number, j: number) => {
296+
if (i < 0 || m <= i || j < 0 || n <= j || [0, 2].includes(grid[i][j])) return;
297+
298+
grid[i][j] = 2;
299+
const dir = [-1, 0, 1, 0, -1];
300+
for (let k = 0; k < 4; k++) {
301+
const [y, x] = [i + dir[k], j + dir[k + 1]];
302+
dfs(y, x);
303+
}
304+
};
305+
306+
const count = () => {
307+
let c = 0;
308+
309+
for (let i = 0; i < m; i++) {
310+
for (let j = 0; j < n; j++) {
311+
if (grid[i][j] === 1) {
312+
dfs(i, j);
313+
c++;
314+
}
315+
}
316+
}
317+
318+
for (let i = 0; i < m; i++) {
319+
for (let j = 0; j < n; j++) {
320+
if (grid[i][j] === 2) {
321+
grid[i][j] = 1;
322+
}
323+
}
324+
}
325+
326+
return c;
327+
};
328+
329+
if (count() !== 1) return 0;
330+
331+
for (let i = 0; i < m; i++) {
332+
for (let j = 0; j < n; j++) {
333+
if (grid[i][j] === 1) {
334+
grid[i][j] = 0;
335+
336+
if (count() !== 1) return 1;
337+
338+
grid[i][j] = 1;
339+
}
340+
}
341+
}
342+
343+
return 2;
344+
}
345+
```
346+
289347
#### JavaScript
290348

291349
```js
@@ -294,54 +352,42 @@ func minDays(grid [][]int) int {
294352
* @return {number}
295353
*/
296354
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-
}
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;
321361
}
322-
}
323362

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);
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);
332378
}
333379
}
334380
}
335-
return count;
336-
}
381+
return c;
382+
};
337383

338-
if (countIslands() !== 1) return 0;
384+
if (count() !== 1) return 0;
339385

340-
for (let i = 0; i < rows; i++) {
341-
for (let j = 0; j < cols; j++) {
386+
for (let i = 0; i < m; i++) {
387+
for (let j = 0; j < n; j++) {
342388
if (grid[i][j] === 1) {
343389
grid[i][j] = 0;
344-
if (countIslands() !== 1) return 1;
390+
if (count() !== 1) return 1;
345391
grid[i][j] = 1;
346392
}
347393
}

‎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)

AltStyle によって変換されたページ (->オリジナル) /