package leetcode;

/**
 * https://leetcode.com/problems/longest-consecutive-sequence/
 */
public class Problem128 {
    public int longestConsecutive(int[] nums) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem128 prob = new Problem128();
        System.out.println(new int[]{100, 4, 200, 1, 3, 2}); // 4
        System.out.println(new int[]{100, 4, 200, 1, 3, 2, 101, 99}); // 4
    }
}

@@ -12,16 +12,113 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A
 /**%0A * h
@@ -266,270 +266,1690 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem128 prob = new Problem128();%0A        System.out.println(new int%5B%5D%7B100, 4, 200, 1, 3, 2%7D); // 4%0A        System.out.println(new int%5B%5D%7B100, 4, 200, 1, 3, 2, 101, 99%7D); // 4
+Map%3CInteger, Integer%3E numToGroupMap = new HashMap%3C%3E();%0A        Map%3CInteger, Set%3CInteger%3E%3E groupToNumsMap = new HashMap%3C%3E();%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            int num = nums%5Bi%5D;%0A            int prevNum = num - 1;%0A            int nextNum = num + 1;%0A%0A            if (numToGroupMap.containsKey(num)) %7B%0A                continue;%0A            %7D%0A            numToGroupMap.put(num, i);%0A            if (groupToNumsMap.containsKey(i)) %7B%0A                groupToNumsMap.get(i).add(num);%0A            %7D else %7B%0A                Set%3CInteger%3E set = new HashSet%3C%3E();%0A                set.add(num);%0A                groupToNumsMap.put(i, set);%0A            %7D%0A            if (numToGroupMap.containsKey(prevNum)) %7B%0A                link(numToGroupMap, groupToNumsMap, num, prevNum);%0A            %7D%0A            if (numToGroupMap.containsKey(nextNum)) %7B%0A                link(numToGroupMap, groupToNumsMap, num, nextNum);%0A            %7D%0A        %7D%0A        int max = 0;%0A        for (Set%3CInteger%3E set : groupToNumsMap.values()) %7B%0A            max = Math.max(max, set.size());%0A        %7D%0A        return max;%0A    %7D%0A%0A    private void link(Map%3CInteger, Integer%3E numToGroupMap,%0A                      Map%3CInteger, Set%3CInteger%3E%3E groupToNumsMap,%0A                      int num,%0A                      int consNum) %7B%0A        int group = numToGroupMap.get(num);%0A        int consGroup = numToGroupMap.get(consNum);%0A        Set%3CInteger%3E consSet = groupToNumsMap.get(consGroup);%0A        consSet.add(num);%0A        Set%3CInteger%3E set = groupToNumsMap.get(group);%0A        consSet.addAll(set);%0A        for (int n : set) %7B%0A            numToGroupMap.put(n, consGroup);%0A        %7D%0A        groupToNumsMap.remove(group);
 %0A   

Solve problem 128