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 1 commit
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
Prev Previous commit
Next Next commit
feat: add 2nd ts solution to lc problem: No.0959
  • Loading branch information
rain84 committed Aug 10, 2024
commit a0250982c68416fcd9e40809653e3c6736ef129e
86 changes: 86 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 @@ -390,4 +390,90 @@ function regionsBySlashes(grid) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
88 changes: 87 additions & 1 deletion solution/0900-0999/0959.Regions Cut By Slashes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Union-Find

<!-- tabs:start -->

Expand Down Expand Up @@ -380,4 +380,90 @@ function regionsBySlashes(grid) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
71 changes: 71 additions & 0 deletions solution/0900-0999/0959.Regions Cut By Slashes/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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;
}