力扣刷题打开03 按公因数计算最大组件大小(困难)

题目:

给定一个由不同正整数的组成的非空数组 nums ,考虑下面的图:

有 nums.length 个节点,按从 nums[0] 到 nums[nums.length - 1] 标记;
只有当 nums[i] 和 nums[j] 共用一个大于 1 的公因数时,nums[i] 和 nums[j]之间才有一条边。
返回 图中最大连通组件的大小 。

示例 1:

输入:nums = [4,6,15,35]
输出:4
示例 2:

输入:nums = [20,50,9,63]
输出:2
示例 3:

输入:nums = [2,3,6,7,4,12,21,39]
输出:8
 

提示:

1 <= nums.length <= 2 * 104
1 <= nums[i] <= 105
nums 中所有值都 不同

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/largest-component-size-by-common-factor
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

答案:

class Solution {

    public int largestComponentSize(int[] nums) {

        int m = Arrays.stream(nums).max().getAsInt();

        //将数组转化为流——最大值——作为Int类型的数据赋值给m

        UnionFind uf = new UnionFind(m + 1);//并查集类型(下面会定义)

        //并查集,将两个数组合并,询问两个元素是否在一个集合里

        for (int num : nums) {//等同于for(int num=num.begin();num<=nums.end();num++)

            for (int i = 2; i*i <= num; i++) {

                if (num % i == 0) {

                    uf.union(num, i);//union()连接两结点

                    uf.union(num, num / i);

                }

            }

        }

        int[] counts = new int[m + 1];

        int ans = 0;

        for (int num : nums) {

            int root = uf.find(num);//查找父节点

            counts[root]++;

            ans = Math.max(ans, counts[root]);

        }

        return ans;

    }

}

class UnionFind {

    int[] parent;

    int[] rank;

    public UnionFind(int n) {

        parent = new int[n];

        for (int i = 0; i < n; i++) {

            parent[i] = i;

        }

        rank = new int[n];

    }

    public void union(int x, int y) {

        int rootx = find(x);

        int rooty = find(y);

        if (rootx != rooty) {

            if (rank[rootx] > rank[rooty]) {

                parent[rooty] = rootx;

            } else if (rank[rootx] < rank[rooty]) {

                parent[rootx] = rooty;

            } else {

                parent[rooty] = rootx;

                rank[rootx]++;

            }

        }

    }

    public int find(int x) {

        if (parent[x] != x) {

            parent[x] = find(parent[x]);

        }

        return parent[x];

    }

}

结论:

不会写...希望未来能有一天敲出来吧...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于力扣刷题C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷题中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷题中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷题中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷题中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷题中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷题中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷题中,使用 string 可以方便地处理字符串相关的问题。 9. 注意边界条件:在力扣刷题中,边界条件往往是解决问题的关键。需要仔细分析题目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷题中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值