diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README.md b/solution/0900-0999/0959.Regions Cut By Slashes/README.md index 6c7ce33e17e65..313507c727c20 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README.md @@ -279,6 +279,62 @@ func regionsBySlashes(grid []string) int { } ``` +#### JavaScript + +```js +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = x => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` + diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md index 826f54d8a3194..33990633ac8fb 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md +++ b/solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md @@ -269,6 +269,62 @@ func regionsBySlashes(grid []string) int { } ``` +#### JavaScript + +```js +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = x => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +} +``` + diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js new file mode 100644 index 0000000000000..93275ea7a9b91 --- /dev/null +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.js @@ -0,0 +1,51 @@ +/** + * @param {string[]} grid + * @return {number} + */ + +function regionsBySlashes(grid) { + const find = x => { + if (p[x] !== x) { + p[x] = find(p[x]); + } + return p[x]; + }; + + const union = (a, b) => { + const pa = find(a); + const pb = find(b); + if (pa !== pb) { + p[pa] = pb; + size--; + } + }; + + const n = grid.length; + let size = n * n * 4; + const p = Array.from({ length: size }, (_, i) => i); + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + const k = i * n + j; + if (i < n - 1) { + union(4 * k + 2, (k + n) * 4); + } + if (j < n - 1) { + union(4 * k + 1, (k + 1) * 4 + 3); + } + if (grid[i][j] === '/') { + union(4 * k, 4 * k + 3); + union(4 * k + 1, 4 * k + 2); + } else if (grid[i][j] === '\\') { + union(4 * k, 4 * k + 1); + union(4 * k + 2, 4 * k + 3); + } else { + union(4 * k, 4 * k + 1); + union(4 * k + 1, 4 * k + 2); + union(4 * k + 2, 4 * k + 3); + } + } + } + + return size; +}