Skip to content

feat: add js/ts solution to lc problem: No.0959 #3398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions solution/0900-0999/0959.Regions Cut By Slashes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,57 @@ func regionsBySlashes(grid []string) int {
}
```

#### TypeScript

```ts
function regionsBySlashes(grid: string[]): number {
const find = (x: number) => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};

const union = (a: number, b: number) => {
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;
}
```

#### JavaScript

```js
Expand Down Expand Up @@ -339,4 +390,166 @@ function regionsBySlashes(grid) {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:DFS

<!-- tabs:start -->

#### TypeScript

```ts
function regionsBySlashes(grid: string[]): number {
const createGraph = () => {
const n = grid.length;
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const [y, x] = [i * 2, j * 2];

switch (grid[i][j]) {
case '/':
g[y][x] = g[y + 1][x + 1] = 0;
g[y][x + 1] = g[y + 1][x] = 1;
break;

case '\\':
g[y][x] = g[y + 1][x + 1] = 2;
g[y][x + 1] = g[y + 1][x] = 0;
break;

default:
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
break;
}
}
}

return g;
};

const isValid = (x: number) => 0 <= x && x < n;
const dfs = (i: number, j: number) => {
if (!isValid(i) || !isValid(j) || g[i][j]) return;

g[i][j] = -1;
const dirs = [-1, 0, 1, 0, -1];
const neighbours: number[] = [];

for (let d = 0; d < 4; d++) {
const [y, x] = [i + dirs[d], j + dirs[d + 1]];

if (isValid(y) && isValid(x)) {
dfs(y, x);
neighbours.push(g[y][x]);
} else {
neighbours.push(-1);
}
}

const [top, right, bottom, left] = neighbours;
if (top === 1 && right === 1) dfs(i - 1, j + 1);
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
if (top === 2 && left === 2) dfs(i - 1, j - 1);
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
};

const g = createGraph();
const n = g.length;
let res = 0;

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (g[i][j] === 0) {
dfs(i, j);
res++;
}
}
}

return res;
}
```

#### JavaScript

```js
function regionsBySlashes(grid) {
const createGraph = () => {
const n = grid.length;
const g = Array.from({ length: n * 2 }, () => Array(n * 2).fill(0));

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const [y, x] = [i * 2, j * 2];

switch (grid[i][j]) {
case '/':
g[y][x] = g[y + 1][x + 1] = 0;
g[y][x + 1] = g[y + 1][x] = 1;
break;

case '\\':
g[y][x] = g[y + 1][x + 1] = 2;
g[y][x + 1] = g[y + 1][x] = 0;
break;

default:
g[y][x] = g[y][x + 1] = g[y + 1][x] = g[y + 1][x + 1] = 0;
break;
}
}
}

return g;
};

const isValid = x => 0 <= x && x < n;
const dfs = (i, j) => {
if (!isValid(i) || !isValid(j) || g[i][j]) return;

g[i][j] = -1;
const dirs = [-1, 0, 1, 0, -1];
const neighbours = [];

for (let d = 0; d < 4; d++) {
const [y, x] = [i + dirs[d], j + dirs[d + 1]];

if (isValid(y) && isValid(x)) {
dfs(y, x);
neighbours.push(g[y][x]);
} else {
neighbours.push(-1);
}
}

const [top, right, bottom, left] = neighbours;
if (top === 1 && right === 1) dfs(i - 1, j + 1);
if (bottom === 1 && left === 1) dfs(i + 1, j - 1);
if (top === 2 && left === 2) dfs(i - 1, j - 1);
if (bottom === 2 && right === 2) dfs(i + 1, j + 1);
};

const g = createGraph();
const n = g.length;
let res = 0;

for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (g[i][j] === 0) {
dfs(i, j);
res++;
}
}
}

return res;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading
Loading