LeetCode--128--hard--LongestConsecutiveSequence

summary:

approach 1 : union find , O(N2lgN) time complexity

approach 2 : sort , O(nlgN) time complexity

approach 3 : hash map, O(N) time complexity

package com.odyssey.app.algorithm.lc.unionfind;

import java.util.*;

/**
 *
 * 128
 * hard
 * https://leetcode.com/problems/longest-consecutive-sequence/
 *
 * Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
 *
 * Your algorithm should run in O(n) complexity.
 *
 * Example:
 *
 * Input: [100, 4, 200, 1, 3, 2]
 * Output: 4
 * Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
 *
 *
 * @author Dingsheng Huang
 * @date 2020/3/31 19:03
 */
public class LongestConsecutiveSequence {

    // approach 1 : union find,  O(N2lgN)
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        // remove duplicates
        Set<Integer> set = new HashSet<>();
        for (int i : nums) {
            set.add(i);
        }
        Integer[] nums2 = new Integer[set.size()];
        set.toArray(nums2);
        UnionFind unionFind = new UnionFind();
        unionFind.init(nums2.length);
        for (int i = 0; i < nums2.length; i++) {
            for (int j = i; j < nums2.length; j++) {
                if (Math.abs(nums2[i] - nums2[j]) == 1) {
                    unionFind.union(i, j);
                }
            }
        }

        return unionFind.max;
    }

    class UnionFind {
        int count;
        int[] id;
        int[] rank;
        int max = 1;


        private void init(int cn) {
            id = new int[cn];
            rank = new int[cn];
            for (int i = 0; i < cn; i++) {
                id[i] = i;
                rank[i] = 1;
                count++;
            }
        }

        private int find(int p) {
            while (p != id[p]) {
                p = id[p];
            }
            return p;
        }

        private void union(int p, int q) {
            int pRoot = find(p);
            int qRoot = find(q);
            if (pRoot == qRoot) {
                return;
            }
            if (pRoot > qRoot) {
                id[qRoot] = pRoot;
                rank[pRoot] += rank[qRoot];
                max = Math.max(max, rank[pRoot]);
            } else {
                id[pRoot] = qRoot;
                rank[qRoot] += rank[pRoot];
                max = Math.max(max, rank[qRoot]);
            }
        }
    }


    // approach2 : sort , O(NlgN)
    public int longestConsecutive2(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        Arrays.sort(nums);
        int result = 0;
        int curr = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == nums[i - 1] + 1) {
                curr++;
                result = Math.max(result, curr);
            } else if (nums[i] == nums[i - 1]) {
                continue;
            } else {
                curr = 1;
            }
        }

        return result;
    }

    // approach 3 : hash map , O(N)
    public int longestConsecutive3(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        Map<Integer, Integer> map = new HashMap<>();
        int result = 1;
        for (int i : nums) {
            if (!map.containsKey(i)) {
                int l = map.containsKey(i - 1) ? map.get(i - 1) : 0;
                int r = map.containsKey(i + 1) ? map.get(i + 1) : 0;
                // len : length of the sequence i is in
                int len = l + r + 1;
                result = Math.max(result, len);
                map.put(i, len);

                // extend the boundary of the sequence
                map.put(i - l, len);
                map.put(i + r, len);
            } else {
                // duplicates
                continue;
            }
        }
        return result;
    }
}

发布了205 篇原创文章 · 获赞 27 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/huangdingsheng/article/details/105230941
今日推荐