Skip to content

Commit ac07a33

Browse files
authored
feat: add js/ts solution to lc problem: No.0959 (doocs#3398)
1 parent 587c048 commit ac07a33

File tree

5 files changed

+615
-1
lines changed

5 files changed

+615
-1
lines changed

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

+213
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,57 @@ func regionsBySlashes(grid []string) int {
279279
}
280280
```
281281

282+
#### TypeScript
283+
284+
```ts
285+
function regionsBySlashes(grid: string[]): number {
286+
const find = (x: number) => {
287+
if (p[x] !== x) {
288+
p[x] = find(p[x]);
289+
}
290+
return p[x];
291+
};
292+
293+
const union = (a: number, b: number) => {
294+
const pa = find(a);
295+
const pb = find(b);
296+
if (pa !== pb) {
297+
p[pa] = pb;
298+
size--;
299+
}
300+
};
301+
302+
const n = grid.length;
303+
let size = n * n * 4;
304+
const p = Array.from({ length: size }, (_, i) => i);
305+
306+
for (let i = 0; i < n; i++) {
307+
for (let j = 0; j < n; j++) {
308+
const k = i * n + j;
309+
if (i < n - 1) {
310+
union(4 * k + 2, (k + n) * 4);
311+
}
312+
if (j < n - 1) {
313+
union(4 * k + 1, (k + 1) * 4 + 3);
314+
}
315+
if (grid[i][j] === '/') {
316+
union(4 * k, 4 * k + 3);
317+
union(4 * k + 1, 4 * k + 2);
318+
} else if (grid[i][j] === '\\') {
319+
union(4 * k, 4 * k + 1);
320+
union(4 * k + 2, 4 * k + 3);
321+
} else {
322+
union(4 * k, 4 * k + 1);
323+
union(4 * k + 1, 4 * k + 2);
324+
union(4 * k + 2, 4 * k + 3);
325+
}
326+
}
327+
}
328+
329+
return size;
330+
}
331+
```
332+
282333
#### JavaScript
283334

284335
```js
@@ -339,4 +390,166 @@ function regionsBySlashes(grid) {
339390

340391
<!-- solution:end -->
341392

393+
<!-- solution:start -->
394+
395+
### 方法二: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+
#### 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+
551+
<!-- tabs:end -->
552+
553+
<!-- solution:end -->
554+
342555
<!-- problem:end -->

0 commit comments

Comments
 (0)