Skip to content

Commit c38745a

Browse files
committed
feat: add 2nd js solution to lc problem: No.0959
1 parent a025098 commit c38745a

File tree

3 files changed

+223
-0
lines changed

3 files changed

+223
-0
lines changed

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,82 @@ function regionsBySlashes(grid: string[]): number {
472472
}
473473
```
474474

475+
#### JavaScript
476+
477+
```js
478+
function regionsBySlashes(grid) {
479+
const createGraph = () => {
480+
const n = grid.length;
481+
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));
482+
483+
for (let i = 0; i < n; i++) {
484+
for (let j = 0; j < n; j++) {
485+
const [y, x] = [i * 2, j * 2];
486+
487+
switch (grid[i][j]) {
488+
case '/':
489+
g[y][x] = g[y + 1][x + 1] = 0;
490+
g[y][x + 1] = g[y + 1][x] = 1;
491+
break;
492+
493+
case '\\':
494+
g[y][x] = g[y + 1][x + 1] = 2;
495+
g[y][x + 1] = g[y + 1][x] = 0;
496+
break;
497+
498+
default:
499+
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
500+
break;
501+
}
502+
}
503+
}
504+
505+
return g;
506+
};
507+
508+
const isValid = x => 0 <= x && x < n;
509+
const dfs = (i, j) => {
510+
if (!isValid(i) || !isValid(j) || g[i][j]) return;
511+
512+
g[i][j] = -1;
513+
const dirs = [-1, 0, 1, 0, -1];
514+
const neighbours = [];
515+
516+
for (let d = 0; d < 4; d++) {
517+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
518+
519+
if (isValid(y) && isValid(x)) {
520+
dfs(y, x);
521+
neighbours.push(g[y][x]);
522+
} else {
523+
neighbours.push(-1);
524+
}
525+
}
526+
527+
const [top, right, bottom, left] = neighbours;
528+
if (top === 1 && right === 1) dfs(i - 1, j + 1);
529+
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
530+
if (top === 2 && left === 2) dfs(i - 1, j - 1);
531+
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
532+
};
533+
534+
const g = createGraph();
535+
const n = g.length;
536+
let res = 0;
537+
538+
for (let i = 0; i < n; i++) {
539+
for (let j = 0; j < n; j++) {
540+
if (g[i][j] === 0) {
541+
dfs(i, j);
542+
res++;
543+
}
544+
}
545+
}
546+
547+
return res;
548+
}
549+
```
550+
475551
<!-- tabs:end -->
476552

477553
<!-- solution:end -->

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,82 @@ function regionsBySlashes(grid: string[]): number {
462462
}
463463
```
464464

465+
#### JavaScript
466+
467+
```js
468+
function regionsBySlashes(grid) {
469+
const createGraph = () => {
470+
const n = grid.length;
471+
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));
472+
473+
for (let i = 0; i < n; i++) {
474+
for (let j = 0; j < n; j++) {
475+
const [y, x] = [i * 2, j * 2];
476+
477+
switch (grid[i][j]) {
478+
case '/':
479+
g[y][x] = g[y + 1][x + 1] = 0;
480+
g[y][x + 1] = g[y + 1][x] = 1;
481+
break;
482+
483+
case '\\':
484+
g[y][x] = g[y + 1][x + 1] = 2;
485+
g[y][x + 1] = g[y + 1][x] = 0;
486+
break;
487+
488+
default:
489+
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
490+
break;
491+
}
492+
}
493+
}
494+
495+
return g;
496+
};
497+
498+
const isValid = x => 0 <= x && x < n;
499+
const dfs = (i, j) => {
500+
if (!isValid(i) || !isValid(j) || g[i][j]) return;
501+
502+
g[i][j] = -1;
503+
const dirs = [-1, 0, 1, 0, -1];
504+
const neighbours = [];
505+
506+
for (let d = 0; d < 4; d++) {
507+
const [y, x] = [i + dirs[d], j + dirs[d + 1]];
508+
509+
if (isValid(y) && isValid(x)) {
510+
dfs(y, x);
511+
neighbours.push(g[y][x]);
512+
} else {
513+
neighbours.push(-1);
514+
}
515+
}
516+
517+
const [top, right, bottom, left] = neighbours;
518+
if (top === 1 && right === 1) dfs(i - 1, j + 1);
519+
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
520+
if (top === 2 && left === 2) dfs(i - 1, j - 1);
521+
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
522+
};
523+
524+
const g = createGraph();
525+
const n = g.length;
526+
let res = 0;
527+
528+
for (let i = 0; i < n; i++) {
529+
for (let j = 0; j < n; j++) {
530+
if (g[i][j] === 0) {
531+
dfs(i, j);
532+
res++;
533+
}
534+
}
535+
}
536+
537+
return res;
538+
}
539+
```
540+
465541
<!-- tabs:end -->
466542

467543
<!-- solution:end -->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
function regionsBySlashes(grid) {
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 => 0 <= x && x < n;
32+
const dfs = (i, j) => {
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 = [];
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)