Skip to content

feat: add js/ts solutions to lc problem: No.0947 #3455

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 3 commits into from
Aug 30, 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
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ function removeStones(stones: number[][]): number {

<!-- solution:end -->

<!--- solution:start --->
<!-- solution:start -->

### 方法二:并查集(优化)

Expand Down Expand Up @@ -601,4 +601,106 @@ function removeStones(stones: number[][]): number {

<!--- solution:end --->

<!-- solution:start -->

### Solution 3: DFS

<!-- tabs:start -->

#### TypeScript

```ts
function removeStones(stones: number[][]): number {
const n = stones.length;
const g: number[][] = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = (i: number) => {
const seen = new Set<number>();

let q = [i];
while (q.length) {
const qNext: number[] = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
```

#### JavaScript

```js
function removeStones(stones) {
const n = stones.length;
const g = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = i => {
const seen = new Set();

let q = [i];
while (q.length) {
const qNext = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function removeStones(stones: number[][]): number {

<!-- solution:end -->

<!--- solution:start --->
<!-- solution:start -->

### Solution 2: Union-Find (Optimized)

Expand Down Expand Up @@ -600,6 +600,108 @@ function removeStones(stones: number[][]): number {

<!-- tabs:end -->

<!--- solution:end --->
<!-- solution:end -->

<!-- solution:start -->

### Solution 3: DFS

<!-- tabs:start -->

#### TypeScript

```ts
function removeStones(stones: number[][]): number {
const n = stones.length;
const g: number[][] = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = (i: number) => {
const seen = new Set<number>();

let q = [i];
while (q.length) {
const qNext: number[] = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
```

#### JavaScript

```js
function removeStones(stones) {
const n = stones.length;
const g = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = i => {
const seen = new Set();

let q = [i];
while (q.length) {
const qNext = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function removeStones(stones) {
const n = stones.length;
const g = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = i => {
const seen = new Set();

let q = [i];
while (q.length) {
const qNext = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function removeStones(stones: number[][]): number {
const n = stones.length;
const g: number[][] = Array.from({ length: n }, () => []);

for (let i = 0; i < n; i++) {
const [y, x] = stones[i];
for (let j = i + 1; j < n; j++) {
if (y === stones[j][0] || x === stones[j][1]) {
g[i].push(j);
g[j].push(i);
}
}
}

const dfs = (i: number) => {
const seen = new Set<number>();

let q = [i];
while (q.length) {
const qNext: number[] = [];

for (const i of q) {
if (seen.has(i)) continue;
seen.add(i);
set.delete(i);
qNext.push(...g[i]);
}

q = qNext;
}
};

const set = new Set(Array.from({ length: n }, (_, i) => i));
let ans = n;
for (const i of set) {
dfs(i);
ans--;
}

return ans;
}
Loading