@@ -0,0 +1,1489 @@
+// Solution runs in O(n * log(n)) time and O(n) space (because of sorting).%0A// Interestingly, beats 95.19%25 of submissions.%0Aclass Solution %7B%0A  public int longestConsecutive(int%5B%5D nums) %7B%0A    if (nums.length == 0) return 0;%0A    Arrays.sort(nums);%0A    int len = 1, tmp = 1;%0A    for (int i = 1; i %3C nums.length; i++) %7B%0A      if (nums%5Bi%5D - nums%5Bi-1%5D == 1) %7B%0A        ++tmp;%0A      %7D else if (nums%5Bi%5D != nums%5Bi-1%5D) %7B%0A        tmp = 1;%0A      %7D%0A      len = Math.max(len, tmp);%0A    %7D%0A    return len;%0A  %7D%0A%7D%0A%0A// Solution can be viewed as similar to graph %22connected components%22 problem.%0A// Solution runs in O(n) time, assuming that set operations take O(1) and queue dequeue and%0A// enqueue operations take O(1) time. It takes O(n) space to store set of elements and%0A// queue of O(n) elements when all elements are consecutive.%0Aclass Solution %7B%0A  public int longestConsecutive(int%5B%5D nums) %7B%0A    HashSet%3CInteger%3E set = new HashSet%3CInteger%3E();%0A    for (int i = 0; i %3C nums.length; i++) %7B%0A      set.add(nums%5Bi%5D);%0A    %7D%0A%0A    LinkedList%3CInteger%3E queue = new LinkedList%3CInteger%3E();%0A    int len = 0;%0A    while (!set.isEmpty()) %7B%0A      int elem = set.iterator().next();%0A      queue.add(elem);%0A      int curr_len = 0;%0A      while (!queue.isEmpty()) %7B%0A        int tmp = queue.remove();%0A        set.remove(tmp);%0A        if (set.contains(tmp + 1)) queue.add(tmp + 1);%0A        if (set.contains(tmp - 1)) queue.add(tmp - 1);%0A        ++curr_len;%0A      %7D%0A      len = Math.max(curr_len, len);%0A    %7D%0A    return len;%0A  %7D%0A%7D%0A

add solution