From 94fcef9de1f9fb6dc70b34b8114de3e0335f0c25 Mon Sep 17 00:00:00 2001 From: rain84 Date: Fri, 30 Aug 2024 00:01:57 +0300 Subject: [PATCH 1/3] feat: add js/ts solutions to lc problem: No.0947 --- .../README.md | 102 ++++++++++++++++++ .../README_EN.md | 102 ++++++++++++++++++ .../Solution3.js | 41 +++++++ .../Solution3.ts | 41 +++++++ 4 files changed, 286 insertions(+) create mode 100644 solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.js create mode 100644 solution/0900-0999/0947.Most Stones Removed with Same Row or Column/Solution3.ts 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..7f6b7d0da953c 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 @@ -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..828d098c9edf8 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 @@ -602,4 +602,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/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; +} From 520182ba4c1bbfa4481d9dee08ec80d9275bb4b1 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 30 Aug 2024 19:52:25 +0800 Subject: [PATCH 2/3] Update README.md --- .../README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 7f6b7d0da953c..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 { - + ### 方法二:并查集(优化) @@ -701,6 +701,6 @@ function removeStones(stones) { - + From e8124e6b528d458358f90362045a2cc4a3a054c2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 30 Aug 2024 19:53:19 +0800 Subject: [PATCH 3/3] Update README_EN.md --- .../README_EN.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 828d098c9edf8..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,7 +600,7 @@ function removeStones(stones: number[][]): number { - + @@ -702,6 +702,6 @@ function removeStones(stones) { - +