力扣第128题最长的连续序列

128.最长连续序列
难度:困难
标签:并查集

给定一个未排序的整数数组,找出最长连续序列的长度。

要求算法的时间复杂度为 O(n)。
示例:

输入: [100, 4, 200, 1, 3, 2]
输出: 4
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。

思路:运用并查集,使用hashmap fatherset key是数组的值,value是key的父结点
初始化时,一共n个树。还有一个hashmap size 放的是每棵树的大小结点个数
遍历数组,然后把判断nums[i]-1是否在 fatherset中 在的话,合并两个结点,合并的时候,需要更新最大的树的结点个数

public int longestConsecutive(int[] nums){
    if(nums==null||nums.length==0)
        return 0;
    UF uf=new UF(nums);
    for (int i = 0; i < nums.length; i++) {
        if(uf.fatherset.containsKey(nums[i]-1)){
            uf.union(nums[i],nums[i]-1);
        }
    }
    return uf.max;
}
class UF{
    public int max=1;
    private int count;
    public HashMap<Integer,Integer> fatherset=new HashMap<>();
    public HashMap<Integer,Integer> size=new HashMap<>();
    public UF(int[] nums){
        for(int i=0;i<nums.length;i++){
            fatherset.put(nums[i],nums[i]);
            size.put(nums[i],1);
        }
        count=nums.length;
    }
    public int find(int p){
        int father=fatherset.get(p);
        if(p!=father) {
            father=find(father);
            fatherset.replace(p,father);
        }
        return father;
    }
    public void union(int p,int q){
        int proot=find(p);
        int qroot=find(q);
        if(proot==qroot) return;
        if(size.get(proot)<size.get(qroot)){
            fatherset.replace(proot,qroot);
            size.replace(qroot,size.get(qroot)+size.get(proot));
            max=Math.max(max,size.get(qroot));
        }
        else {
            fatherset.replace(qroot,proot);
            size.replace(proot,size.get(qroot)+size.get(proot));
            max=Math.max(max,size.get(proot));
        }
        count--;
    }

准备一个hashset,将所有的num放入到里面
遍历时,只有当num-1不在set里面时,说明出现了新的序列
就寻找这个序列的长度,然后更新最大的长度

public int longestConsecutive(int[] nums) {
    Set<Integer> num_set = new HashSet<Integer>();
    for (int num : nums) {
        num_set.add(num);
    }

    int longestStreak = 0;

    for (int num : num_set) {
        if (!num_set.contains(num-1)) {
            int currentNum = num;
            int currentStreak = 1;

            while (num_set.contains(currentNum+1)) {
                currentNum += 1;
                currentStreak += 1;
            }

            longestStreak = Math.max(longestStreak, currentStreak);
        }
    }

    return longestStreak;
}
在LeetCode(力扣)上,有一个经典的算法目叫做“最长连续序列”(Longest Continuous Increasing Subsequence),通常用C++或其他编程语言进行解答。这个目的目的是找到一个给定整数数组中的最长递增子序列。递增子序列是数组中的一段连续元素,它们按顺序严格增大。 这里是一个简单的C++解决方案思路: ```cpp #include <vector> using namespace std; class Solution { public: int longestContinuousIncreasingSubsequence(vector<int>& nums) { if (nums.empty()) return 0; // 避免空数组的情况 int n = nums.size(); vector<int> dp(n, 1); // dp[i] 表示以nums[i]结尾的最长递增子序列长度 int max_len = 1; // 初始化最长递增子序列长度为1 for (int i = 1; i < n; ++i) { // 遍历数组,从第二个元素开始 if (nums[i] > nums[i-1]) { // 如果当前元素比前一个大 dp[i] = dp[i-1] + 1; // 更新dp值,考虑加入当前元素后的增长长度 max_len = max(max_len, dp[i]); // 检查是否更新了最长子序列长度 } } return max_len; // 返回最长连续递增子序列的长度 } }; ``` 在这个代码中,我们使用了一个动态规划(Dynamic Programming)的方法,维护了一个数组`dp`来存储每个位置以该位置元素结尾的最大递增子序列长度。遍历过程中,如果遇到当前元素大于前一个元素,则说明可以形成一个新的递增子序列,所以将`dp[i]`设置为`dp[i-1]+1`,并更新全局的最长子序列长度。 如果你想要深入了解这个问,可以问: 1. 这个问的时间复杂度是多少? 2. 动态规划是如何帮助解决这个问的? 3. 如何优化这个算法,使其空间复杂度更低?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值