Skip to content

Commit 0727ef5

Browse files
committed
https://leetcode.cn/problems/regions-cut-by-slashes/
1 parent fbbe204 commit 0727ef5

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/regions-cut-by-slashes/
14+
1315
https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
1416

1517
https://leetcode.cn/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/

regions-cut-by-slashes/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { UnionFind } from "../largest-component-size-by-common-factor/UnionFind.ts";
2+
export default function regionsBySlashes(grid: string[]): number {
3+
const uf = new UnionFind();
4+
const n = grid.length;
5+
for (const [i, s] of grid.entries()) {
6+
const row = [...s];
7+
for (const [j, c] of row.entries()) {
8+
const index = 4 * (i * n + j);
9+
if (c === "/") {
10+
uf.union(index, index + 3);
11+
uf.union(index + 1, index + 2);
12+
} else if (c === "\\") {
13+
uf.union(index, index + 1);
14+
uf.union(index + 2, index + 3);
15+
} else {
16+
uf.union(index, index + 1);
17+
uf.union(index + 2, index + 3);
18+
uf.union(index + 1, index + 2);
19+
}
20+
if (j + 1 < n) {
21+
uf.union(index + 1, 4 * (i * n + j + 1) + 3);
22+
}
23+
if (i + 1 < n) {
24+
uf.union(index + 2, 4 * ((i + 1) * n + j));
25+
}
26+
}
27+
}
28+
return [...new Array(n * n * 4).keys()].reduce(
29+
(p, c) => p + Number(uf.find(c) === c),
30+
0,
31+
);
32+
}

regions-cut-by-slashes/test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
2+
import regionsBySlashes from "./index.ts";
3+
Deno.test("regions-cut-by-slashes", () => {
4+
assertEquals(
5+
[
6+
[
7+
" // /\\/ /\\ \\ /\\\\ \\ / / \\/\\",
8+
"/\\ ///\\/\\/// \\//\\ \\ /\\\\ \\",
9+
"\\ / / /\\/\\\\\\ \\// /\\\\/\\/\\ ",
10+
"/// // /\\\\\\\\/ ///\\/\\/ /\\ /",
11+
"/\\//\\\\// / /\\/ \\//\\/\\\\ \\// ",
12+
"/\\ /////\\\\ /\\//\\/\\\\ /\\ /",
13+
"/ \\\\ \\\\/\\//\\/\\/ //////\\ \\/",
14+
"\\\\/\\ \\//\\//\\ \\ \\//\\/\\/\\/\\\\\\\\ ",
15+
" \\//\\ \\ / / / // /\\ /\\\\ \\\\/\\",
16+
"///\\ /\\/\\\\\\\\ / \\/ \\/\\\\\\ \\// ",
17+
"/ /\\\\ \\ \\\\/\\\\ \\\\ / /\\\\// //\\",
18+
"/\\/\\///\\/\\//\\\\/\\\\ \\/ \\\\//\\ \\ \\",
19+
"/\\// / \\/ // \\////\\ \\\\/ \\\\ \\/ ",
20+
"/ \\\\\\ //\\\\/\\/\\\\\\/\\\\ \\// /\\\\\\/",
21+
" \\ ///\\/\\ \\\\ \\\\\\\\/ // // ",
22+
"\\/\\ / /\\ / /\\\\/\\\\ \\\\\\ /\\\\/ \\\\",
23+
"\\ // /\\/ ////\\\\\\/ / \\\\\\ / \\",
24+
"\\/ \\\\/ \\//\\\\ \\// //\\/ /\\\\\\\\ ",
25+
" / / / / \\\\//\\\\ \\/ ///\\\\",
26+
"\\\\ \\\\\\/\\ \\/ /\\/ //\\\\\\/ \\ ",
27+
"//////\\ /\\/// //\\/\\\\\\ /\\\\ //",
28+
"\\\\\\ // \\ /\\ // /\\\\ / ///\\\\ /",
29+
"/\\\\ //\\/\\\\// \\ \\ \\\\/\\\\\\\\\\ \\/",
30+
"\\/\\ \\\\/// \\ \\//\\/ \\/\\/ / \\ \\/\\",
31+
"/ /\\\\/\\/\\/ \\\\/ \\\\/ / \\ \\\\ \\",
32+
"/\\/\\ / /\\ //\\/\\\\//\\/\\/ /\\\\ /\\",
33+
"\\ \\//\\\\/\\ /\\ /\\//\\// ///\\ \\ ",
34+
" \\\\/ \\//\\ \\\\ // / \\/\\\\//\\/",
35+
" \\/\\ \\\\/\\ / \\ / \\\\\\/ \\\\/\\ ",
36+
" \\\\\\//\\\\//\\/ /\\\\/ \\\\// \\\\\\ /\\",
37+
],
38+
[" /", "/ "],
39+
[" /", " "],
40+
["/\\", "\\/"],
41+
[" /", "/ "],
42+
[
43+
" \\ \\/ /// //\\ \\ \\ /// /\\\\ //\\",
44+
" ///\\ \\////\\ /\\\\/\\\\///\\// \\ /",
45+
"// // /\\ /\\ \\ //\\ \\\\\\\\///\\/\\ ",
46+
" \\\\\\\\///\\\\/ \\/\\\\ \\\\\\/// / ",
47+
" / / \\\\ \\\\/ \\\\ /\\/ //\\ / ",
48+
"/ / //// / \\ //\\ /// /// \\ ",
49+
"/\\/\\//\\ \\\\\\\\\\ \\\\// // \\/ \\\\ ",
50+
" //\\ \\\\\\/\\/\\\\ /\\ /\\/// /\\ //\\",
51+
"//\\\\\\\\// / \\\\ /\\/ //\\//\\/\\\\ ",
52+
" /\\ //\\\\/ /\\/\\//\\ / //\\ \\/ /",
53+
" \\/ \\///\\/\\\\/ \\ \\ \\// \\//\\ ",
54+
" / ///\\/\\\\\\/\\\\ \\ /\\ / \\",
55+
"/\\\\/\\ ////\\/ / / /\\/// / /\\",
56+
" \\\\\\ \\ / \\ /\\ // // / ",
57+
" \\/ \\\\ /\\\\// /\\\\\\ /\\ /\\\\\\ ",
58+
"\\/ \\/\\ /// /\\ / \\ / // / \\ \\",
59+
"\\/\\\\\\ \\//\\ \\ \\\\ \\/ // // /\\",
60+
"/ \\/\\\\\\ // \\\\\\/ \\ /\\/ \\\\\\ ",
61+
"\\\\ \\/\\ // /\\ // \\\\/\\\\ \\\\ \\ /",
62+
"/ \\\\ / \\\\\\/\\\\ \\ / \\ \\ / \\",
63+
"/ / \\ \\\\ \\ /\\ /\\ /\\\\// \\\\// ",
64+
"/ //// \\/\\// /\\/ / \\ \\\\\\ \\/ ",
65+
"\\ ////\\/\\ \\ ////\\//\\/ \\/\\ /",
66+
"/ \\ // \\ / \\\\ /\\ //// / /\\",
67+
"/ //\\/ / \\/ \\/\\\\ /\\ /\\ \\//\\",
68+
"\\/\\\\ \\////\\\\/\\/ //\\ / /\\\\\\\\ ",
69+
"/////\\\\\\/ \\ \\ /\\/\\ / /\\// ",
70+
"\\ / // \\//\\\\/\\\\\\ \\//\\ /// ",
71+
"// / / / / /\\\\/\\\\/\\\\\\ \\ \\\\",
72+
" \\\\ \\ /// \\\\/\\\\ /\\ / ///\\/",
73+
],
74+
[
75+
" \\\\ // /\\/\\/ // /\\ \\\\ \\//",
76+
" \\/\\//\\\\//\\\\\\\\//// /\\/ \\\\/\\/\\/",
77+
"\\\\\\ /\\// \\\\/ /\\ /\\\\//\\/\\////",
78+
" //\\\\ \\/ /////\\ /\\/ //\\/ \\/ /",
79+
" //// / / \\//\\\\ \\\\/\\\\/\\\\\\// \\",
80+
"\\\\\\\\ / /\\/\\\\\\/\\ \\\\//\\ \\ \\ //\\",
81+
"//\\\\// /\\///\\\\\\\\\\ / \\ /\\/ \\\\",
82+
"\\// \\\\\\/ //\\ /\\ // / \\\\ / / ",
83+
" / /// \\// \\\\ \\\\ ///\\\\// ",
84+
" \\ \\\\/ /\\/\\\\ \\ \\ \\\\/\\//\\\\",
85+
"\\\\ \\ /\\ \\/\\\\/ \\\\\\//\\ /\\ /",
86+
" \\\\ \\ \\\\\\\\\\\\/\\ \\/ \\// \\",
87+
"\\/ \\ /\\ /\\ \\\\\\/\\ //\\/\\\\ \\\\\\\\",
88+
"\\\\/ / \\\\// \\/\\\\ \\\\/ \\\\ \\/ ",
89+
" / \\/\\ \\\\// \\///\\\\/ //\\\\/\\//\\",
90+
" \\\\ \\/\\/ \\/\\ \\ // /\\ \\\\\\\\/ \\",
91+
" \\ \\ /\\\\ /\\\\ ///\\ /\\ /\\ \\//",
92+
"/ / / // /// /\\ // / \\ \\ / /",
93+
"\\/\\\\ \\/\\\\\\ ///\\///\\\\/\\//// \\//",
94+
"///\\\\\\ \\/ \\\\/\\/\\\\//\\ \\ \\/\\/",
95+
"\\\\\\ /// // \\ \\\\/\\/ \\/\\ / / ",
96+
" /\\//\\\\ \\/\\ \\/\\/\\\\\\ \\ \\//\\ \\\\",
97+
"/\\\\ \\//\\\\ / / \\ \\ //\\\\ \\ \\\\ \\ ",
98+
"/\\/\\/\\ //\\\\ // // / / \\\\\\/ \\/",
99+
"/\\\\ /\\\\/ / //\\ // / /\\\\ \\ ",
100+
"\\\\\\//\\\\///\\/ \\\\ \\ \\\\ / \\\\///\\",
101+
"/ /\\/\\\\\\///\\/\\\\///// / /\\ ",
102+
"\\ \\/\\ /\\/ \\//\\\\ \\\\ \\/\\ \\\\/\\",
103+
"//\\ / \\ / /\\\\/////\\\\ \\ /\\ ",
104+
"/ / // /\\///\\ \\ \\ / /\\ \\ ",
105+
],
106+
].map(regionsBySlashes),
107+
[37, 2, 1, 5, 2, 32, 41],
108+
);
109+
});

0 commit comments

Comments
 (0)