Skip to content

Commit

Permalink
feat: add solutions to lc problem: No. 3378
Browse files Browse the repository at this point in the history
  • Loading branch information
karygauss03 committed Jan 7, 2025
1 parent c039cc7 commit 55482be
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ typedef struct DSU {

class Solution {
public:
int countComponents(vector<int> &nums, int threshold) {
int countComponents(vector<int>& 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<int> par;
for (auto &num : nums) {
for (auto& num : nums) {
if (num > threshold) {
par.insert(num);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Solution {
private:
void dfs(int node, vector<vector<int>> &adj, vector<bool> &vis) {
void dfs(int node, vector<vector<int>>& adj, vector<bool>& 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<int> &nums, int threshold) {
int countComponents(vector<int>& nums, int threshold) {
vector<vector<int>> adj(threshold + 1);
vector<bool> vis(threshold + 1, false);
int ans = 0;
for (auto &num : nums) {
for (auto& num : nums) {
if (num > threshold) {
++ans;
continue;
Expand All @@ -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;
Expand Down

0 comments on commit 55482be

Please sign in to comment.