From 511e9a1679b8e2e30ff90e46369a1f802a8abc65 Mon Sep 17 00:00:00 2001 From: Karim Omrane <72917840+karygauss03@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:43:05 +0000 Subject: [PATCH 1/5] feat: add solutions to lc problem: No. 3378 --- .../README.md | 394 ++++++++++++++++- .../README_EN.md | 396 +++++++++++++++++- .../Solution1.cpp | 54 +++ .../Solution1.go | 64 +++ .../Solution1.java | 66 +++ .../Solution1.py | 41 ++ .../Solution2.cpp | 33 ++ .../Solution2.go | 43 ++ .../Solution2.java | 40 ++ .../Solution2.py | 27 ++ 10 files changed, 1151 insertions(+), 7 deletions(-) create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java create mode 100644 solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.py diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md index bb9c5a4837c19..f4cdfae1174c0 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md @@ -89,25 +89,413 @@ tags: #### Python3 ```python - +class DSU: + def __init__(self, n): + self.parent = {i: i for i in range(n)} + self.rank = {i: 0 for i in range(n)} + + def make_set(self, v): + self.parent[v] = v + self.rank[v] = 1 + + def find(self, x): + if self.parent[x] != x: + self.parent[x] = self.find(self.parent[x]) + return self.parent[x] + + def union_set(self, u, v): + u = self.find(u) + v = self.find(v) + if u != v: + if self.rank[u] < self.rank[v]: + u, v = v, u + self.parent[v] = u + if self.rank[u] == self.rank[v]: + self.rank[u] += 1 + + +class Solution: + def countComponents(self, nums, threshold): + dsu = DSU(threshold + 1) + + for num in nums: + for j in range(num, threshold + 1, num): + dsu.union_set(num, j) + + unique_parents = set() + for num in nums: + if num > threshold: + unique_parents.add(num) + else: + unique_parents.add(dsu.find(num)) + + return len(unique_parents) ``` #### Java ```java - +import java.util.*; + +class DSU { + private Map parent; + private Map rank; + + public DSU(int n) { + parent = new HashMap<>(); + rank = new HashMap<>(); + for (int i = 0; i <= n; i++) { + parent.put(i, i); + rank.put(i, 0); + } + } + + public void makeSet(int v) { + parent.put(v, v); + rank.put(v, 1); + } + + public int find(int x) { + if (parent.get(x) != x) { + parent.put(x, find(parent.get(x))); + } + return parent.get(x); + } + + public void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank.get(u) < rank.get(v)) { + int temp = u; + u = v; + v = temp; + } + parent.put(v, u); + if (rank.get(u).equals(rank.get(v))) { + rank.put(u, rank.get(u) + 1); + } + } + } +} + +class Solution { + public int countComponents(int[] nums, int threshold) { + DSU dsu = new DSU(threshold); + + for (int num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + + Set uniqueParents = new HashSet<>(); + for (int num : nums) { + if (num > threshold) { + uniqueParents.add(num); + } else { + uniqueParents.add(dsu.find(num)); + } + } + + return uniqueParents.size(); + } +} ``` #### C++ ```cpp - +typedef struct DSU +{ + unordered_map par, rank; + DSU(int n) { + for (int i = 0; i < n; ++i) { + par[i] = i; + rank[i] = 0; + } + } + + void makeSet(int v) { + par[v] = v; + rank[v] = 1; + } + + int find(int x) { + if (par[x] == x) { + return x; + } + return par[x] = find(par[x]); + } + + void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank[u] < rank[v]) swap(u, v); + par[v] = u; + if (rank[u] == rank[v]) rank[u]++; + } + } +}DSU; + +class Solution { +public: + int countComponents(vector& nums, int threshold) { + DSU dsu(threshold); + for(auto& num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + unordered_set par; + for (auto& num : nums) { + if (num > threshold) { + par.insert(num); + } + else { + par.insert(dsu.find(num)); + } + } + return par.size(); + } +}; ``` #### Go ```go +package main + +import ( + "fmt" +) + +type DSU struct { + parent map[int]int + rank map[int]int +} + +func NewDSU(n int) *DSU { + dsu := &DSU{ + parent: make(map[int]int), + rank: make(map[int]int), + } + for i := 0; i <= n; i++ { + dsu.parent[i] = i + dsu.rank[i] = 0 + } + return dsu +} + +func (dsu *DSU) Find(x int) int { + if dsu.parent[x] != x { + dsu.parent[x] = dsu.Find(dsu.parent[x]) + } + return dsu.parent[x] +} + +func (dsu *DSU) Union(u, v int) { + uRoot := dsu.Find(u) + vRoot := dsu.Find(v) + if uRoot != vRoot { + if dsu.rank[uRoot] < dsu.rank[vRoot] { + uRoot, vRoot = vRoot, uRoot + } + dsu.parent[vRoot] = uRoot + if dsu.rank[uRoot] == dsu.rank[vRoot] { + dsu.rank[uRoot]++ + } + } +} + +func countComponents(nums []int, threshold int) int { + dsu := NewDSU(threshold) + + for _, num := range nums { + for j := num; j <= threshold; j += num { + dsu.Union(num, j) + } + } + + uniqueParents := make(map[int]struct{}) + for _, num := range nums { + if num > threshold { + uniqueParents[num] = struct{}{} + } else { + uniqueParents[dsu.Find(num)] = struct{}{} + } + } + + return len(uniqueParents) +} +``` + +### Solution 2 (DFS) + + +#### Python3 + +```python +class Solution: + def dfs(self, node, adj, vis): + if vis[node]: + return + vis[node] = True + for neighbor in adj[node]: + self.dfs(neighbor, adj, vis) + + def countComponents(self, nums, threshold): + adj = [[] for _ in range(threshold + 1)] + vis = [False] * (threshold + 1) + ans = 0 + + for num in nums: + if num > threshold: + ans += 1 + continue + for j in range(2 * num, threshold + 1, num): + adj[num].append(j) + adj[j].append(num) + + for num in nums: + if num <= threshold and not vis[num]: + self.dfs(num, adj, vis) + ans += 1 + + return ans +``` + +#### Java + +```java +import java.util.*; + +class Solution { + private void dfs(int node, List> adj, boolean[] visited) { + if (visited[node]) return; + visited[node] = true; + for (int neighbor : adj.get(node)) { + dfs(neighbor, adj, visited); + } + } + + public int countComponents(int[] nums, int threshold) { + List> adj = new ArrayList<>(); + for (int i = 0; i <= threshold; i++) { + adj.add(new ArrayList<>()); + } + boolean[] visited = new boolean[threshold + 1]; + int ans = 0; + + for (int num : nums) { + if (num > threshold) { + ans++; + continue; + } + for (int j = 2 * num; j <= threshold; j += num) { + adj.get(num).add(j); + adj.get(j).add(num); + } + } + + for (int num : nums) { + if (num <= threshold && !visited[num]) { + dfs(num, adj, visited); + ans++; + } + } + + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +private: + void dfs(int node, vector>& adj, vector& vis) { + if (vis[node]) return; + vis[node] = true; + for (auto& u : adj[node]) { + dfs(u, adj, vis); + } + } +public: + int countComponents(vector& nums, int threshold) { + vector> adj(threshold + 1); + vector vis(threshold + 1, false); + int ans = 0; + for (auto& num : nums) { + if (num > threshold) { + ++ans; + continue; + } + for (int j = 2*num; j <= threshold; j += num) { + adj[num].push_back(j); + adj[j].push_back(num); + } + } + for (auto& num : nums) { + if (num <= threshold && !vis[num]) { + dfs(num, adj, vis); + ++ans; + } + } + return ans; + } +}; +``` + +#### Go + +```go +package main + +import "fmt" + +func dfs(node int, adj [][]int, visited []bool) { + if visited[node] { + return + } + visited[node] = true + for _, neighbor := range adj[node] { + dfs(neighbor, adj, visited) + } +} + +func countComponents(nums []int, threshold int) int { + adj := make([][]int, threshold+1) + for i := range adj { + adj[i] = []int{} + } + + visited := make([]bool, threshold+1) + components := 0 + + for _, num := range nums { + if num > threshold { + components++ + continue + } + for j := 2 * num; j <= threshold; j += num { + adj[num] = append(adj[num], j) + adj[j] = append(adj[j], num) + } + } + + for _, num := range nums { + if num <= threshold && !visited[num] { + dfs(num, adj, visited) + components++ + } + } + + return components +} ``` diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md index 4a89be0905dc9..554c9e296f85f 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md @@ -79,32 +79,420 @@ tags: -### Solution 1 +### Solution 1 (DSU) #### Python3 ```python - +class DSU: + def __init__(self, n): + self.parent = {i: i for i in range(n)} + self.rank = {i: 0 for i in range(n)} + + def make_set(self, v): + self.parent[v] = v + self.rank[v] = 1 + + def find(self, x): + if self.parent[x] != x: + self.parent[x] = self.find(self.parent[x]) + return self.parent[x] + + def union_set(self, u, v): + u = self.find(u) + v = self.find(v) + if u != v: + if self.rank[u] < self.rank[v]: + u, v = v, u + self.parent[v] = u + if self.rank[u] == self.rank[v]: + self.rank[u] += 1 + + +class Solution: + def countComponents(self, nums, threshold): + dsu = DSU(threshold + 1) + + for num in nums: + for j in range(num, threshold + 1, num): + dsu.union_set(num, j) + + unique_parents = set() + for num in nums: + if num > threshold: + unique_parents.add(num) + else: + unique_parents.add(dsu.find(num)) + + return len(unique_parents) ``` #### Java ```java - +import java.util.*; + +class DSU { + private Map parent; + private Map rank; + + public DSU(int n) { + parent = new HashMap<>(); + rank = new HashMap<>(); + for (int i = 0; i <= n; i++) { + parent.put(i, i); + rank.put(i, 0); + } + } + + public void makeSet(int v) { + parent.put(v, v); + rank.put(v, 1); + } + + public int find(int x) { + if (parent.get(x) != x) { + parent.put(x, find(parent.get(x))); + } + return parent.get(x); + } + + public void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank.get(u) < rank.get(v)) { + int temp = u; + u = v; + v = temp; + } + parent.put(v, u); + if (rank.get(u).equals(rank.get(v))) { + rank.put(u, rank.get(u) + 1); + } + } + } +} + +class Solution { + public int countComponents(int[] nums, int threshold) { + DSU dsu = new DSU(threshold); + + for (int num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + + Set uniqueParents = new HashSet<>(); + for (int num : nums) { + if (num > threshold) { + uniqueParents.add(num); + } else { + uniqueParents.add(dsu.find(num)); + } + } + + return uniqueParents.size(); + } +} ``` #### C++ ```cpp - +typedef struct DSU +{ + unordered_map par, rank; + DSU(int n) { + for (int i = 0; i < n; ++i) { + par[i] = i; + rank[i] = 0; + } + } + + void makeSet(int v) { + par[v] = v; + rank[v] = 1; + } + + int find(int x) { + if (par[x] == x) { + return x; + } + return par[x] = find(par[x]); + } + + void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank[u] < rank[v]) swap(u, v); + par[v] = u; + if (rank[u] == rank[v]) rank[u]++; + } + } +}DSU; + +class Solution { +public: + int countComponents(vector& nums, int threshold) { + DSU dsu(threshold); + for(auto& num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + unordered_set par; + for (auto& num : nums) { + if (num > threshold) { + par.insert(num); + } + else { + par.insert(dsu.find(num)); + } + } + return par.size(); + } +}; ``` #### Go ```go +package main + +import ( + "fmt" +) + +type DSU struct { + parent map[int]int + rank map[int]int +} + +func NewDSU(n int) *DSU { + dsu := &DSU{ + parent: make(map[int]int), + rank: make(map[int]int), + } + for i := 0; i <= n; i++ { + dsu.parent[i] = i + dsu.rank[i] = 0 + } + return dsu +} + +func (dsu *DSU) Find(x int) int { + if dsu.parent[x] != x { + dsu.parent[x] = dsu.Find(dsu.parent[x]) + } + return dsu.parent[x] +} + +func (dsu *DSU) Union(u, v int) { + uRoot := dsu.Find(u) + vRoot := dsu.Find(v) + if uRoot != vRoot { + if dsu.rank[uRoot] < dsu.rank[vRoot] { + uRoot, vRoot = vRoot, uRoot + } + dsu.parent[vRoot] = uRoot + if dsu.rank[uRoot] == dsu.rank[vRoot] { + dsu.rank[uRoot]++ + } + } +} + +func countComponents(nums []int, threshold int) int { + dsu := NewDSU(threshold) + + for _, num := range nums { + for j := num; j <= threshold; j += num { + dsu.Union(num, j) + } + } + + uniqueParents := make(map[int]struct{}) + for _, num := range nums { + if num > threshold { + uniqueParents[num] = struct{}{} + } else { + uniqueParents[dsu.Find(num)] = struct{}{} + } + } + + return len(uniqueParents) +} +``` + +### Solution 2 (DFS) + + +#### Python3 + +```python +class Solution: + def dfs(self, node, adj, vis): + if vis[node]: + return + vis[node] = True + for neighbor in adj[node]: + self.dfs(neighbor, adj, vis) + + def countComponents(self, nums, threshold): + adj = [[] for _ in range(threshold + 1)] + vis = [False] * (threshold + 1) + ans = 0 + + for num in nums: + if num > threshold: + ans += 1 + continue + for j in range(2 * num, threshold + 1, num): + adj[num].append(j) + adj[j].append(num) + + for num in nums: + if num <= threshold and not vis[num]: + self.dfs(num, adj, vis) + ans += 1 + + return ans +``` + +#### Java + +```java +import java.util.*; + +class Solution { + private void dfs(int node, List> adj, boolean[] visited) { + if (visited[node]) return; + visited[node] = true; + for (int neighbor : adj.get(node)) { + dfs(neighbor, adj, visited); + } + } + + public int countComponents(int[] nums, int threshold) { + List> adj = new ArrayList<>(); + for (int i = 0; i <= threshold; i++) { + adj.add(new ArrayList<>()); + } + boolean[] visited = new boolean[threshold + 1]; + int ans = 0; + + for (int num : nums) { + if (num > threshold) { + ans++; + continue; + } + for (int j = 2 * num; j <= threshold; j += num) { + adj.get(num).add(j); + adj.get(j).add(num); + } + } + + for (int num : nums) { + if (num <= threshold && !visited[num]) { + dfs(num, adj, visited); + ans++; + } + } + + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +private: + void dfs(int node, vector>& adj, vector& vis) { + if (vis[node]) return; + vis[node] = true; + for (auto& u : adj[node]) { + dfs(u, adj, vis); + } + } +public: + int countComponents(vector& nums, int threshold) { + vector> adj(threshold + 1); + vector vis(threshold + 1, false); + int ans = 0; + for (auto& num : nums) { + if (num > threshold) { + ++ans; + continue; + } + for (int j = 2*num; j <= threshold; j += num) { + adj[num].push_back(j); + adj[j].push_back(num); + } + } + for (auto& num : nums) { + if (num <= threshold && !vis[num]) { + dfs(num, adj, vis); + ++ans; + } + } + return ans; + } +}; +``` + +#### Go + +```go +package main + +import "fmt" + +func dfs(node int, adj [][]int, visited []bool) { + if visited[node] { + return + } + visited[node] = true + for _, neighbor := range adj[node] { + dfs(neighbor, adj, visited) + } +} + +func countComponents(nums []int, threshold int) int { + adj := make([][]int, threshold+1) + for i := range adj { + adj[i] = []int{} + } + + visited := make([]bool, threshold+1) + components := 0 + + for _, num := range nums { + if num > threshold { + components++ + continue + } + for j := 2 * num; j <= threshold; j += num { + adj[num] = append(adj[num], j) + adj[j] = append(adj[j], num) + } + } + + for _, num := range nums { + if num <= threshold && !visited[num] { + dfs(num, adj, visited) + components++ + } + } + + return components +} ``` diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp new file mode 100644 index 0000000000000..07ef50e81967d --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp @@ -0,0 +1,54 @@ +typedef struct DSU +{ + unordered_map par, rank; + DSU(int n) { + for (int i = 0; i < n; ++i) { + par[i] = i; + rank[i] = 0; + } + } + + void makeSet(int v) { + par[v] = v; + rank[v] = 1; + } + + int find(int x) { + if (par[x] == x) { + return x; + } + return par[x] = find(par[x]); + } + + void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank[u] < rank[v]) swap(u, v); + par[v] = u; + if (rank[u] == rank[v]) rank[u]++; + } + } +}DSU; + +class Solution { +public: + int countComponents(vector& nums, int threshold) { + DSU dsu(threshold); + for(auto& num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + unordered_set par; + for (auto& num : nums) { + if (num > threshold) { + par.insert(num); + } + else { + par.insert(dsu.find(num)); + } + } + return par.size(); + } +}; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go new file mode 100644 index 0000000000000..85dc8226a8c14 --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" +) + +type DSU struct { + parent map[int]int + rank map[int]int +} + +func NewDSU(n int) *DSU { + dsu := &DSU{ + parent: make(map[int]int), + rank: make(map[int]int), + } + for i := 0; i <= n; i++ { + dsu.parent[i] = i + dsu.rank[i] = 0 + } + return dsu +} + +func (dsu *DSU) Find(x int) int { + if dsu.parent[x] != x { + dsu.parent[x] = dsu.Find(dsu.parent[x]) + } + return dsu.parent[x] +} + +func (dsu *DSU) Union(u, v int) { + uRoot := dsu.Find(u) + vRoot := dsu.Find(v) + if uRoot != vRoot { + if dsu.rank[uRoot] < dsu.rank[vRoot] { + uRoot, vRoot = vRoot, uRoot + } + dsu.parent[vRoot] = uRoot + if dsu.rank[uRoot] == dsu.rank[vRoot] { + dsu.rank[uRoot]++ + } + } +} + +func countComponents(nums []int, threshold int) int { + dsu := NewDSU(threshold) + + for _, num := range nums { + for j := num; j <= threshold; j += num { + dsu.Union(num, j) + } + } + + uniqueParents := make(map[int]struct{}) + for _, num := range nums { + if num > threshold { + uniqueParents[num] = struct{}{} + } else { + uniqueParents[dsu.Find(num)] = struct{}{} + } + } + + return len(uniqueParents) +} diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java new file mode 100644 index 0000000000000..b04fb99111a0e --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java @@ -0,0 +1,66 @@ +import java.util.*; + +class DSU { + private Map parent; + private Map rank; + + public DSU(int n) { + parent = new HashMap<>(); + rank = new HashMap<>(); + for (int i = 0; i <= n; i++) { + parent.put(i, i); + rank.put(i, 0); + } + } + + public void makeSet(int v) { + parent.put(v, v); + rank.put(v, 1); + } + + public int find(int x) { + if (parent.get(x) != x) { + parent.put(x, find(parent.get(x))); + } + return parent.get(x); + } + + public void unionSet(int u, int v) { + u = find(u); + v = find(v); + if (u != v) { + if (rank.get(u) < rank.get(v)) { + int temp = u; + u = v; + v = temp; + } + parent.put(v, u); + if (rank.get(u).equals(rank.get(v))) { + rank.put(u, rank.get(u) + 1); + } + } + } +} + +class Solution { + public int countComponents(int[] nums, int threshold) { + DSU dsu = new DSU(threshold); + + for (int num : nums) { + for (int j = num; j <= threshold; j += num) { + dsu.unionSet(num, j); + } + } + + Set uniqueParents = new HashSet<>(); + for (int num : nums) { + if (num > threshold) { + uniqueParents.add(num); + } else { + uniqueParents.add(dsu.find(num)); + } + } + + return uniqueParents.size(); + } +} diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py new file mode 100644 index 0000000000000..d6a1aed4b39b6 --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py @@ -0,0 +1,41 @@ +class DSU: + def __init__(self, n): + self.parent = {i: i for i in range(n)} + self.rank = {i: 0 for i in range(n)} + + def make_set(self, v): + self.parent[v] = v + self.rank[v] = 1 + + def find(self, x): + if self.parent[x] != x: + self.parent[x] = self.find(self.parent[x]) + return self.parent[x] + + def union_set(self, u, v): + u = self.find(u) + v = self.find(v) + if u != v: + if self.rank[u] < self.rank[v]: + u, v = v, u + self.parent[v] = u + if self.rank[u] == self.rank[v]: + self.rank[u] += 1 + + +class Solution: + def countComponents(self, nums, threshold): + dsu = DSU(threshold + 1) + + for num in nums: + for j in range(num, threshold + 1, num): + dsu.union_set(num, j) + + unique_parents = set() + for num in nums: + if num > threshold: + unique_parents.add(num) + else: + unique_parents.add(dsu.find(num)) + + return len(unique_parents) diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp new file mode 100644 index 0000000000000..9f1b31cef07a1 --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp @@ -0,0 +1,33 @@ +class Solution { +private: + void dfs(int node, vector>& adj, vector& vis) { + if (vis[node]) return; + vis[node] = true; + for (auto& u : adj[node]) { + dfs(u, adj, vis); + } + } +public: + int countComponents(vector& nums, int threshold) { + vector> adj(threshold + 1); + vector vis(threshold + 1, false); + int ans = 0; + for (auto& num : nums) { + if (num > threshold) { + ++ans; + continue; + } + for (int j = 2*num; j <= threshold; j += num) { + adj[num].push_back(j); + adj[j].push_back(num); + } + } + for (auto& num : nums) { + if (num <= threshold && !vis[num]) { + dfs(num, adj, vis); + ++ans; + } + } + return ans; + } +}; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go new file mode 100644 index 0000000000000..4915d0f742d13 --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go @@ -0,0 +1,43 @@ +package main + +import "fmt" + +func dfs(node int, adj [][]int, visited []bool) { + if visited[node] { + return + } + visited[node] = true + for _, neighbor := range adj[node] { + dfs(neighbor, adj, visited) + } +} + +func countComponents(nums []int, threshold int) int { + adj := make([][]int, threshold+1) + for i := range adj { + adj[i] = []int{} + } + + visited := make([]bool, threshold+1) + components := 0 + + for _, num := range nums { + if num > threshold { + components++ + continue + } + for j := 2 * num; j <= threshold; j += num { + adj[num] = append(adj[num], j) + adj[j] = append(adj[j], num) + } + } + + for _, num := range nums { + if num <= threshold && !visited[num] { + dfs(num, adj, visited) + components++ + } + } + + return components +} diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java new file mode 100644 index 0000000000000..6b081ef6ef763 --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java @@ -0,0 +1,40 @@ +import java.util.*; + +class Solution { + private void dfs(int node, List> adj, boolean[] visited) { + if (visited[node]) return; + visited[node] = true; + for (int neighbor : adj.get(node)) { + dfs(neighbor, adj, visited); + } + } + + public int countComponents(int[] nums, int threshold) { + List> adj = new ArrayList<>(); + for (int i = 0; i <= threshold; i++) { + adj.add(new ArrayList<>()); + } + boolean[] visited = new boolean[threshold + 1]; + int ans = 0; + + for (int num : nums) { + if (num > threshold) { + ans++; + continue; + } + for (int j = 2 * num; j <= threshold; j += num) { + adj.get(num).add(j); + adj.get(j).add(num); + } + } + + for (int num : nums) { + if (num <= threshold && !visited[num]) { + dfs(num, adj, visited); + ans++; + } + } + + return ans; + } +} diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.py b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.py new file mode 100644 index 0000000000000..fce43a2c4f55d --- /dev/null +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.py @@ -0,0 +1,27 @@ +class Solution: + def dfs(self, node, adj, vis): + if vis[node]: + return + vis[node] = True + for neighbor in adj[node]: + self.dfs(neighbor, adj, vis) + + def countComponents(self, nums, threshold): + adj = [[] for _ in range(threshold + 1)] + vis = [False] * (threshold + 1) + ans = 0 + + for num in nums: + if num > threshold: + ans += 1 + continue + for j in range(2 * num, threshold + 1, num): + adj[num].append(j) + adj[j].append(num) + + for num in nums: + if num <= threshold and not vis[num]: + self.dfs(num, adj, vis) + ans += 1 + + return ans From c039cc771d3018a0d7a906bca0a7782a0af203a0 Mon Sep 17 00:00:00 2001 From: Karim Omrane <72917840+karygauss03@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:50:05 +0000 Subject: [PATCH 2/5] feat: add solutions to lc problem: No. 3378 --- .../README.md | 27 +++++++++---------- .../README_EN.md | 27 +++++++++---------- .../Solution1.cpp | 14 +++++----- .../Solution2.cpp | 13 ++++----- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md index f4cdfae1174c0..8e0573b4d8cce 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md @@ -206,8 +206,7 @@ class Solution { #### C++ ```cpp -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -237,23 +236,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } @@ -417,29 +415,30 @@ class Solution { ```cpp class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md index 554c9e296f85f..01379722ebc6c 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md @@ -203,8 +203,7 @@ class Solution { #### C++ ```cpp -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -234,23 +233,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } @@ -414,29 +412,30 @@ class Solution { ```cpp class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp index 07ef50e81967d..8436808daadc2 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp @@ -1,5 +1,4 @@ -typedef struct DSU -{ +typedef struct DSU { unordered_map par, rank; DSU(int n) { for (int i = 0; i < n; ++i) { @@ -29,23 +28,22 @@ typedef struct DSU if (rank[u] == rank[v]) rank[u]++; } } -}DSU; +} DSU; class Solution { public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { DSU dsu(threshold); - for(auto& num : nums) { + for (auto &num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { par.insert(num); - } - else { + } else { par.insert(dsu.find(num)); } } diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp index 9f1b31cef07a1..e398b903a990c 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp @@ -1,28 +1,29 @@ class Solution { private: - void dfs(int node, vector>& adj, vector& vis) { + void dfs(int node, vector> &adj, vector &vis) { if (vis[node]) return; vis[node] = true; - for (auto& u : adj[node]) { + for (auto &u : adj[node]) { dfs(u, adj, vis); } } + public: - int countComponents(vector& nums, int threshold) { + int countComponents(vector &nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto& num : nums) { + for (auto &num : nums) { if (num > threshold) { ++ans; continue; } - for (int j = 2*num; j <= threshold; j += num) { + for (int j = 2 * num; j <= threshold; j += num) { adj[num].push_back(j); adj[j].push_back(num); } } - for (auto& num : nums) { + for (auto &num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; From 55482be757ecb9cf6cef84b0d2f0381217adcbae Mon Sep 17 00:00:00 2001 From: Karim Omrane <72917840+karygauss03@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:56:36 +0000 Subject: [PATCH 3/5] feat: add solutions to lc problem: No. 3378 --- .../Solution1.cpp | 6 +++--- .../Solution2.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp index 8436808daadc2..9d91022a2731d 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp @@ -32,15 +32,15 @@ typedef struct DSU { class Solution { public: - int countComponents(vector &nums, int threshold) { + int countComponents(vector& nums, int threshold) { DSU dsu(threshold); - for (auto &num : nums) { + for (auto& num : nums) { for (int j = num; j <= threshold; j += num) { dsu.unionSet(num, j); } } unordered_set par; - for (auto &num : nums) { + for (auto& num : nums) { if (num > threshold) { par.insert(num); } else { diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp index e398b903a990c..95af4408b0a58 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.cpp @@ -1,19 +1,19 @@ class Solution { private: - void dfs(int node, vector> &adj, vector &vis) { + void dfs(int node, vector>& adj, vector& vis) { if (vis[node]) return; vis[node] = true; - for (auto &u : adj[node]) { + for (auto& u : adj[node]) { dfs(u, adj, vis); } } public: - int countComponents(vector &nums, int threshold) { + int countComponents(vector& nums, int threshold) { vector> adj(threshold + 1); vector vis(threshold + 1, false); int ans = 0; - for (auto &num : nums) { + for (auto& num : nums) { if (num > threshold) { ++ans; continue; @@ -23,7 +23,7 @@ class Solution { adj[j].push_back(num); } } - for (auto &num : nums) { + for (auto& num : nums) { if (num <= threshold && !vis[num]) { dfs(num, adj, vis); ++ans; From 5547db28769281d7c74dec365d927849a87be780 Mon Sep 17 00:00:00 2001 From: Karim Omrane <72917840+karygauss03@users.noreply.github.com> Date: Wed, 8 Jan 2025 08:27:05 +0000 Subject: [PATCH 4/5] feat: add solutions to lc problem: No. 3378 --- .../{Solution1.cpp => Solution.cpp} | 0 .../{Solution1.go => Solution.go} | 0 .../{Solution1.java => Solution.java} | 0 .../{Solution1.py => Solution.py} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename solution/3300-3399/3378.Count Connected Components in LCM Graph/{Solution1.cpp => Solution.cpp} (100%) rename solution/3300-3399/3378.Count Connected Components in LCM Graph/{Solution1.go => Solution.go} (100%) rename solution/3300-3399/3378.Count Connected Components in LCM Graph/{Solution1.java => Solution.java} (100%) rename solution/3300-3399/3378.Count Connected Components in LCM Graph/{Solution1.py => Solution.py} (100%) diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.cpp similarity index 100% rename from solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.cpp rename to solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.cpp diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go similarity index 100% rename from solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.go rename to solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java similarity index 100% rename from solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.java rename to solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.py similarity index 100% rename from solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution1.py rename to solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.py From ad1b86ecbafee608a914456fef03ce29e219e174 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 9 Jan 2025 10:57:59 +0800 Subject: [PATCH 5/5] Remove unnecessary imports and update README. --- .../README.md | 24 +++++++------------ .../README_EN.md | 24 +++++++------------ .../Solution.go | 6 ----- .../Solution.java | 2 -- .../Solution2.go | 4 ---- .../Solution2.java | 2 -- 6 files changed, 16 insertions(+), 46 deletions(-) diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md index 8e0573b4d8cce..b347fb62e52cf 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README.md @@ -82,7 +82,7 @@ tags: -### 方法一 +### 方法一:并查集 @@ -135,8 +135,6 @@ class Solution: #### Java ```java -import java.util.*; - class DSU { private Map parent; private Map rank; @@ -263,12 +261,6 @@ public: #### Go ```go -package main - -import ( - "fmt" -) - type DSU struct { parent map[int]int rank map[int]int @@ -329,7 +321,13 @@ func countComponents(nums []int, threshold int) int { } ``` -### Solution 2 (DFS) + + + + + + +### 方法二:DFS @@ -368,8 +366,6 @@ class Solution: #### Java ```java -import java.util.*; - class Solution { private void dfs(int node, List> adj, boolean[] visited) { if (visited[node]) return; @@ -452,10 +448,6 @@ public: #### Go ```go -package main - -import "fmt" - func dfs(node int, adj [][]int, visited []bool) { if visited[node] { return diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md index 01379722ebc6c..a8f4e5e1d1384 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/README_EN.md @@ -79,7 +79,7 @@ tags: -### Solution 1 (DSU) +### Solution 1: Union Find @@ -132,8 +132,6 @@ class Solution: #### Java ```java -import java.util.*; - class DSU { private Map parent; private Map rank; @@ -260,12 +258,6 @@ public: #### Go ```go -package main - -import ( - "fmt" -) - type DSU struct { parent map[int]int rank map[int]int @@ -326,7 +318,13 @@ func countComponents(nums []int, threshold int) int { } ``` -### Solution 2 (DFS) + + + + + + +### Solution 2: DFS @@ -365,8 +363,6 @@ class Solution: #### Java ```java -import java.util.*; - class Solution { private void dfs(int node, List> adj, boolean[] visited) { if (visited[node]) return; @@ -449,10 +445,6 @@ public: #### Go ```go -package main - -import "fmt" - func dfs(node int, adj [][]int, visited []bool) { if visited[node] { return diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go index 85dc8226a8c14..c52bff9e53b66 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.go @@ -1,9 +1,3 @@ -package main - -import ( - "fmt" -) - type DSU struct { parent map[int]int rank map[int]int diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java index b04fb99111a0e..461914777fde1 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution.java @@ -1,5 +1,3 @@ -import java.util.*; - class DSU { private Map parent; private Map rank; diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go index 4915d0f742d13..bfd25dc4c5b1f 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.go @@ -1,7 +1,3 @@ -package main - -import "fmt" - func dfs(node int, adj [][]int, visited []bool) { if visited[node] { return diff --git a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java index 6b081ef6ef763..8706a7720a362 100644 --- a/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java +++ b/solution/3300-3399/3378.Count Connected Components in LCM Graph/Solution2.java @@ -1,5 +1,3 @@ -import java.util.*; - class Solution { private void dfs(int node, List> adj, boolean[] visited) { if (visited[node]) return;