Skip to content

Commit a025098

Browse files
committed
feat: add 2nd ts solution to lc problem: No.0959
1 parent 4ce8747 commit a025098

File tree

3 files changed

+244
-1
lines changed

3 files changed

+244
-1
lines changed

solution/0900-0999/0959.Regions Cut By Slashes/README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,4 +390,90 @@ function regionsBySlashes(grid) {
390390

391391
<!-- solution:end -->
392392

393+
<!-- solution:start -->
394+
395+
### Solution 2: DFS
396+
397+
<!-- tabs:start -->
398+
399+
#### TypeScript
400+
401+
```ts
402+
function regionsBySlashes(grid: string[]): number {
403+
const createGraph = () => {
404+
const n = grid.length;
405+
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));
406+
407+
for (let i = 0; i < n; i++) {
408+
for (let j = 0; j < n; j++) {
409+
const [y, x] = [i * 2, j * 2];
410+
411+
switch (grid[i][j]) {
412+
case '/':
413+
g[y][x] = g[y + 1][x + 1] = 0;
414+
g[y][x + 1] = g[y + 1][x] = 1;
415+
break;
416+
417+
case '\\':
418+
g[y][x] = g[y + 1][x + 1] = 2;
419+
g[y][x + 1] = g[y + 1][x] = 0;
420+
break;
421+
422+
default:
423+
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
424+
break;
425+
}
426+
}
427+
}
428+
429+
return g;
430+
};
431+
432+
const isValid = (x: number) => 0 <= x && x < n;
433+
const dfs = (i: number, j: number) => {
434+
if (!isValid(i) || !isValid(j) || g[i][j]) return;
435+
436+
g[i][j] = -1;
437+
const dirs = [-1, 0, 1, 0, -1];
438+
const neighbours: number[] = [];
439+
440+
for (let d = 0; d < 4; d++) {
441+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
442+
443+
if (isValid(y) && isValid(x)) {
444+
dfs(y, x);
445+
neighbours.push(g[y][x]);
446+
} else {
447+
neighbours.push(-1);
448+
}
449+
}
450+
451+
const [top, right, bottom, left] = neighbours;
452+
if (top === 1 && right === 1) dfs(i - 1, j + 1);
453+
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
454+
if (top === 2 && left === 2) dfs(i - 1, j - 1);
455+
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
456+
};
457+
458+
const g = createGraph();
459+
const n = g.length;
460+
let res = 0;
461+
462+
for (let i = 0; i < n; i++) {
463+
for (let j = 0; j < n; j++) {
464+
if (g[i][j] === 0) {
465+
dfs(i, j);
466+
res++;
467+
}
468+
}
469+
}
470+
471+
return res;
472+
}
473+
```
474+
475+
<!-- tabs:end -->
476+
477+
<!-- solution:end -->
478+
393479
<!-- problem:end -->

solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Union-Find
6969

7070
<!-- tabs:start -->
7171

@@ -380,4 +380,90 @@ function regionsBySlashes(grid) {
380380

381381
<!-- solution:end -->
382382

383+
<!-- solution:start -->
384+
385+
### Solution 2: DFS
386+
387+
<!-- tabs:start -->
388+
389+
#### TypeScript
390+
391+
```ts
392+
function regionsBySlashes(grid: string[]): number {
393+
const createGraph = () => {
394+
const n = grid.length;
395+
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));
396+
397+
for (let i = 0; i < n; i++) {
398+
for (let j = 0; j < n; j++) {
399+
const [y, x] = [i * 2, j * 2];
400+
401+
switch (grid[i][j]) {
402+
case '/':
403+
g[y][x] = g[y + 1][x + 1] = 0;
404+
g[y][x + 1] = g[y + 1][x] = 1;
405+
break;
406+
407+
case '\\':
408+
g[y][x] = g[y + 1][x + 1] = 2;
409+
g[y][x + 1] = g[y + 1][x] = 0;
410+
break;
411+
412+
default:
413+
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
414+
break;
415+
}
416+
}
417+
}
418+
419+
return g;
420+
};
421+
422+
const isValid = (x: number) => 0 <= x && x < n;
423+
const dfs = (i: number, j: number) => {
424+
if (!isValid(i) || !isValid(j) || g[i][j]) return;
425+
426+
g[i][j] = -1;
427+
const dirs = [-1, 0, 1, 0, -1];
428+
const neighbours: number[] = [];
429+
430+
for (let d = 0; d < 4; d++) {
431+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
432+
433+
if (isValid(y) && isValid(x)) {
434+
dfs(y, x);
435+
neighbours.push(g[y][x]);
436+
} else {
437+
neighbours.push(-1);
438+
}
439+
}
440+
441+
const [top, right, bottom, left] = neighbours;
442+
if (top === 1 && right === 1) dfs(i - 1, j + 1);
443+
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
444+
if (top === 2 && left === 2) dfs(i - 1, j - 1);
445+
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
446+
};
447+
448+
const g = createGraph();
449+
const n = g.length;
450+
let res = 0;
451+
452+
for (let i = 0; i < n; i++) {
453+
for (let j = 0; j < n; j++) {
454+
if (g[i][j] === 0) {
455+
dfs(i, j);
456+
res++;
457+
}
458+
}
459+
}
460+
461+
return res;
462+
}
463+
```
464+
465+
<!-- tabs:end -->
466+
467+
<!-- solution:end -->
468+
383469
<!-- problem:end -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function regionsBySlashes(grid: string[]): number {
2+
const createGraph = () => {
3+
const n = grid.length;
4+
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));
5+
6+
for (let i = 0; i < n; i++) {
7+
for (let j = 0; j < n; j++) {
8+
const [y, x] = [i * 2, j * 2];
9+
10+
switch (grid[i][j]) {
11+
case '/':
12+
g[y][x] = g[y + 1][x + 1] = 0;
13+
g[y][x + 1] = g[y + 1][x] = 1;
14+
break;
15+
16+
case '\\':
17+
g[y][x] = g[y + 1][x + 1] = 2;
18+
g[y][x + 1] = g[y + 1][x] = 0;
19+
break;
20+
21+
default:
22+
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
23+
break;
24+
}
25+
}
26+
}
27+
28+
return g;
29+
};
30+
31+
const isValid = (x: number) => 0 <= x && x < n;
32+
const dfs = (i: number, j: number) => {
33+
if (!isValid(i) || !isValid(j) || g[i][j]) return;
34+
35+
g[i][j] = -1;
36+
const dirs = [-1, 0, 1, 0, -1];
37+
const neighbours: number[] = [];
38+
39+
for (let d = 0; d < 4; d++) {
40+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
41+
42+
if (isValid(y) && isValid(x)) {
43+
dfs(y, x);
44+
neighbours.push(g[y][x]);
45+
} else {
46+
neighbours.push(-1);
47+
}
48+
}
49+
50+
const [top, right, bottom, left] = neighbours;
51+
if (top === 1 && right === 1) dfs(i - 1, j + 1);
52+
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
53+
if (top === 2 && left === 2) dfs(i - 1, j - 1);
54+
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
55+
};
56+
57+
const g = createGraph();
58+
const n = g.length;
59+
let res = 0;
60+
61+
for (let i = 0; i < n; i++) {
62+
for (let j = 0; j < n; j++) {
63+
if (g[i][j] === 0) {
64+
dfs(i, j);
65+
res++;
66+
}
67+
}
68+
}
69+
70+
return res;
71+
}

0 commit comments

Comments
 (0)