Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d2a5bf

Browse files
authored
feat: add js/ts solutions to lc problem: No.0947 (doocs#3455)
1 parent 3a876ec commit 4d2a5bf

File tree

4 files changed

+289
-3
lines changed

4 files changed

+289
-3
lines changed

‎solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ function removeStones(stones: number[][]): number {
338338

339339
<!-- solution:end -->
340340

341-
<!--- solution:start --->
341+
<!-- solution:start -->
342342

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

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

602602
<!--- solution:end --->
603603

604+
<!-- solution:start -->
605+
606+
### Solution 3: DFS
607+
608+
<!-- tabs:start -->
609+
610+
#### TypeScript
611+
612+
```ts
613+
function removeStones(stones: number[][]): number {
614+
const n = stones.length;
615+
const g: number[][] = Array.from({ length: n }, () => []);
616+
617+
for (let i = 0; i < n; i++) {
618+
const [y, x] = stones[i];
619+
for (let j = i + 1; j < n; j++) {
620+
if (y === stones[j][0] || x === stones[j][1]) {
621+
g[i].push(j);
622+
g[j].push(i);
623+
}
624+
}
625+
}
626+
627+
const dfs = (i: number) => {
628+
const seen = new Set<number>();
629+
630+
let q = [i];
631+
while (q.length) {
632+
const qNext: number[] = [];
633+
634+
for (const i of q) {
635+
if (seen.has(i)) continue;
636+
seen.add(i);
637+
set.delete(i);
638+
qNext.push(...g[i]);
639+
}
640+
641+
q = qNext;
642+
}
643+
};
644+
645+
const set = new Set(Array.from({ length: n }, (_, i) => i));
646+
let ans = n;
647+
for (const i of set) {
648+
dfs(i);
649+
ans--;
650+
}
651+
652+
return ans;
653+
}
654+
```
655+
656+
#### JavaScript
657+
658+
```js
659+
function removeStones(stones) {
660+
const n = stones.length;
661+
const g = Array.from({ length: n }, () => []);
662+
663+
for (let i = 0; i < n; i++) {
664+
const [y, x] = stones[i];
665+
for (let j = i + 1; j < n; j++) {
666+
if (y === stones[j][0] || x === stones[j][1]) {
667+
g[i].push(j);
668+
g[j].push(i);
669+
}
670+
}
671+
}
672+
673+
const dfs = i => {
674+
const seen = new Set();
675+
676+
let q = [i];
677+
while (q.length) {
678+
const qNext = [];
679+
680+
for (const i of q) {
681+
if (seen.has(i)) continue;
682+
seen.add(i);
683+
set.delete(i);
684+
qNext.push(...g[i]);
685+
}
686+
687+
q = qNext;
688+
}
689+
};
690+
691+
const set = new Set(Array.from({ length: n }, (_, i) => i));
692+
let ans = n;
693+
for (const i of set) {
694+
dfs(i);
695+
ans--;
696+
}
697+
698+
return ans;
699+
}
700+
```
701+
702+
<!-- tabs:end -->
703+
704+
<!-- solution:end -->
705+
604706
<!-- problem:end -->

‎solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md‎

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function removeStones(stones: number[][]): number {
339339

340340
<!-- solution:end -->
341341

342-
<!--- solution:start --->
342+
<!-- solution:start -->
343343

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

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

601601
<!-- tabs:end -->
602602

603-
<!--- solution:end --->
603+
<!-- solution:end -->
604+
605+
<!-- solution:start -->
606+
607+
### Solution 3: DFS
608+
609+
<!-- tabs:start -->
610+
611+
#### TypeScript
612+
613+
```ts
614+
function removeStones(stones: number[][]): number {
615+
const n = stones.length;
616+
const g: number[][] = Array.from({ length: n }, () => []);
617+
618+
for (let i = 0; i < n; i++) {
619+
const [y, x] = stones[i];
620+
for (let j = i + 1; j < n; j++) {
621+
if (y === stones[j][0] || x === stones[j][1]) {
622+
g[i].push(j);
623+
g[j].push(i);
624+
}
625+
}
626+
}
627+
628+
const dfs = (i: number) => {
629+
const seen = new Set<number>();
630+
631+
let q = [i];
632+
while (q.length) {
633+
const qNext: number[] = [];
634+
635+
for (const i of q) {
636+
if (seen.has(i)) continue;
637+
seen.add(i);
638+
set.delete(i);
639+
qNext.push(...g[i]);
640+
}
641+
642+
q = qNext;
643+
}
644+
};
645+
646+
const set = new Set(Array.from({ length: n }, (_, i) => i));
647+
let ans = n;
648+
for (const i of set) {
649+
dfs(i);
650+
ans--;
651+
}
652+
653+
return ans;
654+
}
655+
```
656+
657+
#### JavaScript
658+
659+
```js
660+
function removeStones(stones) {
661+
const n = stones.length;
662+
const g = Array.from({ length: n }, () => []);
663+
664+
for (let i = 0; i < n; i++) {
665+
const [y, x] = stones[i];
666+
for (let j = i + 1; j < n; j++) {
667+
if (y === stones[j][0] || x === stones[j][1]) {
668+
g[i].push(j);
669+
g[j].push(i);
670+
}
671+
}
672+
}
673+
674+
const dfs = i => {
675+
const seen = new Set();
676+
677+
let q = [i];
678+
while (q.length) {
679+
const qNext = [];
680+
681+
for (const i of q) {
682+
if (seen.has(i)) continue;
683+
seen.add(i);
684+
set.delete(i);
685+
qNext.push(...g[i]);
686+
}
687+
688+
q = qNext;
689+
}
690+
};
691+
692+
const set = new Set(Array.from({ length: n }, (_, i) => i));
693+
let ans = n;
694+
for (const i of set) {
695+
dfs(i);
696+
ans--;
697+
}
698+
699+
return ans;
700+
}
701+
```
702+
703+
<!-- tabs:end -->
704+
705+
<!-- solution:end -->
604706

605707
<!-- problem:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function removeStones(stones) {
2+
const n = stones.length;
3+
const g = Array.from({ length: n }, () => []);
4+
5+
for (let i = 0; i < n; i++) {
6+
const [y, x] = stones[i];
7+
for (let j = i + 1; j < n; j++) {
8+
if (y === stones[j][0] || x === stones[j][1]) {
9+
g[i].push(j);
10+
g[j].push(i);
11+
}
12+
}
13+
}
14+
15+
const dfs = i => {
16+
const seen = new Set();
17+
18+
let q = [i];
19+
while (q.length) {
20+
const qNext = [];
21+
22+
for (const i of q) {
23+
if (seen.has(i)) continue;
24+
seen.add(i);
25+
set.delete(i);
26+
qNext.push(...g[i]);
27+
}
28+
29+
q = qNext;
30+
}
31+
};
32+
33+
const set = new Set(Array.from({ length: n }, (_, i) => i));
34+
let ans = n;
35+
for (const i of set) {
36+
dfs(i);
37+
ans--;
38+
}
39+
40+
return ans;
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function removeStones(stones: number[][]): number {
2+
const n = stones.length;
3+
const g: number[][] = Array.from({ length: n }, () => []);
4+
5+
for (let i = 0; i < n; i++) {
6+
const [y, x] = stones[i];
7+
for (let j = i + 1; j < n; j++) {
8+
if (y === stones[j][0] || x === stones[j][1]) {
9+
g[i].push(j);
10+
g[j].push(i);
11+
}
12+
}
13+
}
14+
15+
const dfs = (i: number) => {
16+
const seen = new Set<number>();
17+
18+
let q = [i];
19+
while (q.length) {
20+
const qNext: number[] = [];
21+
22+
for (const i of q) {
23+
if (seen.has(i)) continue;
24+
seen.add(i);
25+
set.delete(i);
26+
qNext.push(...g[i]);
27+
}
28+
29+
q = qNext;
30+
}
31+
};
32+
33+
const set = new Set(Array.from({ length: n }, (_, i) => i));
34+
let ans = n;
35+
for (const i of set) {
36+
dfs(i);
37+
ans--;
38+
}
39+
40+
return ans;
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /