package leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * https://leetcode.com/problems/longest-consecutive-sequence/
 */
public class Problem128 {
    public int longestConsecutive(int[] nums) {
        Map<Integer, Integer> numToGroupMap = new HashMap<>();
        Map<Integer, Set<Integer>> groupToNumsMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int prevNum = num - 1;
            int nextNum = num + 1;

            if (numToGroupMap.containsKey(num)) {
                continue;
            }
            numToGroupMap.put(num, i);
            if (groupToNumsMap.containsKey(i)) {
                groupToNumsMap.get(i).add(num);
            } else {
                Set<Integer> set = new HashSet<>();
                set.add(num);
                groupToNumsMap.put(i, set);
            }
            if (numToGroupMap.containsKey(prevNum)) {
                link(numToGroupMap, groupToNumsMap, num, prevNum);
            }
            if (numToGroupMap.containsKey(nextNum)) {
                link(numToGroupMap, groupToNumsMap, num, nextNum);
            }
        }
        int max = 0;
        for (Set<Integer> set : groupToNumsMap.values()) {
            max = Math.max(max, set.size());
        }
        return max;
    }

    private void link(Map<Integer, Integer> numToGroupMap,
                      Map<Integer, Set<Integer>> groupToNumsMap,
                      int num,
                      int consNum) {
        int group = numToGroupMap.get(num);
        int consGroup = numToGroupMap.get(consNum);
        Set<Integer> consSet = groupToNumsMap.get(consGroup);
        consSet.add(num);
        Set<Integer> set = groupToNumsMap.get(group);
        consSet.addAll(set);
        for (int n : set) {
            numToGroupMap.put(n, consGroup);
        }
        groupToNumsMap.remove(group);
    }
}

@@ -12,16 +12,44 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -92,16 +92,39 @@
 ashSet;%0A
+import java.util.List;%0A
 import j
@@ -313,27 +313,27 @@
         
-Map
+Set
 %3CInteger
 , Intege
@@ -328,32 +328,13 @@
 eger
-, Integer%3E numToGroupMap
+%3E set
  = n
@@ -336,33 +336,100 @@
 t = new Hash
-Map
+Set
 %3C%3E();%0A
+        for (int num : nums) %7B%0A            set.add(num);%0A        %7D%0A
         Map%3C
@@ -429,34 +429,35 @@
    Map%3CInteger, 
-Se
+Lis
 t%3CInteger%3E%3E grou
@@ -454,28 +454,19 @@
 ger%3E%3E gr
-oupToNumsM
 ap
+h
  = new H
@@ -498,35 +498,18 @@
 int 
-i = 0; i %3C nums.length; i++
+num : nums
 ) %7B%0A
@@ -528,21 +528,19 @@
 int 
-num
+a
  = num
-s%5Bi%5D
+ - 1
 ;%0A  
@@ -557,23 +557,17 @@
 int 
-prevNum
+b
  = num 
--
++
  1;%0A
@@ -582,31 +582,50 @@
     
-int nextNum = num + 1;%0A
+graph.putIfAbsent(num, new ArrayList%3C%3E());
 %0A   
@@ -633,37 +633,27 @@
         if (
-numToGroupMap
+set
 .containsKey
@@ -653,15 +653,10 @@
 ains
-Key(num
+(a
 )) %7B
@@ -676,16 +676,29 @@
     
-continue
+graph.get(num).add(a)
 ;%0A  
@@ -725,65 +725,15 @@
     
-numToGroupMap.put(num, i);%0A            if (groupToNumsMap
+if (set
 .con
@@ -741,13 +741,10 @@
 ains
-Key(i
+(b
 )) %7B
@@ -766,35 +766,26 @@
   gr
-oupToNumsM
 ap
+h
 .get(
-i
+num
 ).add(
-num
+b
 );%0A 
@@ -800,15 +800,8 @@
    %7D
- else %7B
 %0A   
@@ -797,32 +797,34 @@
       %7D%0A        
+%7D%0A
         Set%3CInte
@@ -820,35 +820,39 @@
    Set%3CInteger%3E 
-set
+visited
  = new HashSet%3C%3E
@@ -867,492 +867,179 @@
     
-        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());
+int answer = 0;%0A        for (int num : nums) %7B%0A            if (!visited.contains(num)) %7B%0A                answer = Math.max(answer, dfs(graph, visited, num));%0A            %7D
 %0A   
@@ -1060,19 +1060,22 @@
  return 
-max
+answer
 ;%0A    %7D%0A
@@ -1091,17 +1091,22 @@
 ate 
-void link
+static int dfs
 (Map
@@ -1115,24 +1115,29 @@
 nteger, 
+List%3C
 Integer%3E
  numToGr
@@ -1132,543 +1132,296 @@
 ger%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 = groupToNumsM
+%3E graph, Set%3CInteger%3E visited, int num) %7B%0A        visited.add(num);%0A        int count = 0;%0A        for (int neighbor : gr
 ap
+h
 .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)
+num)) %7B%0A            if (!visited.contains(neighbor)) %7B%0A                count += dfs(graph, visited, neighbor);%0A            %7D%0A        %7D%0A        return count + 1
 ;%0A  

Update problem 128