diff --git a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md index 90f7b5ef95b96..c0d8a249190ea 100644 --- a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md +++ b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README.md @@ -338,7 +338,7 @@ function removeStones(stones: number[][]): number { - + ### 方法二:并查集(优化) @@ -601,4 +601,106 @@ function removeStones(stones: number[][]): number { + + +### Solution 3: DFS + + + +#### 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(); + + 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; +} +``` + + + + + diff --git a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md index 500e5ced4bc29..fca8168604266 100644 --- a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md +++ b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/README_EN.md @@ -339,7 +339,7 @@ function removeStones(stones: number[][]): number { - + ### Solution 2: Union-Find (Optimized) @@ -600,6 +600,108 @@ function removeStones(stones: number[][]): number { - + + + + +### Solution 3: DFS + + + +#### 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(); + + 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; +} +``` + + + + diff --git a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.js b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.js new file mode 100644 index 0000000000000..95b73545eaccd --- /dev/null +++ b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.js @@ -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; +} diff --git a/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.ts b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.ts new file mode 100644 index 0000000000000..353041dabe7d7 --- /dev/null +++ b/solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.ts @@ -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(); + + 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; +}