[LintCode] Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

Clarification

Your algorithm should run in O(n) complexity.

Example

Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

 

Solution 1.  O(n * log n) runtime using sorting 

Algorithm:

1. sort the array;

2. iterate through the sorted array and process each element in the following 3 possible cases.

  a. num[i] - num[i - 1] == 1, currMaxLen++;

  b. num[i] - num[i - 1] != 1 && num[i] != num[i - 1], the current sequence discontinues, reset currMaxLen to 1;

  c. num[i] == num[i - 1], the current sequence should continue, do nothing. 

 

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         }
 6         Arrays.sort(num);
 7         int maxLen = 1, currMaxLen = 1;
 8         for(int i = 1; i < num.length; i++){
 9             if(num[i] - num[i - 1] == 1){
10                 currMaxLen++;
11             }
12             else if(num[i] != num[i - 1]){
13                 currMaxLen = 1;
14             }
15             maxLen = Math.max(maxLen, currMaxLen);
16         }
17         return maxLen;
18     }
19 }

 

Solution 2. O(n * log n) runtime, O(n) space using min priority queue.

Similiarly with solution 1, we can use a min pq to replace the effect of sorting.

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         }
 6         PriorityQueue<Integer> minPq = new PriorityQueue<Integer>(num.length);
 7         for(int i = 0; i < num.length; i++){
 8             minPq.add(num[i]);
 9         }
10         int prevMin = Integer.MAX_VALUE, currMin = Integer.MIN_VALUE;
11         int currMaxLen = 1, maxLen = Integer.MIN_VALUE;
12         while(minPq.size() != 0){
13             currMin = minPq.poll();
14             if(currMin - prevMin == 1){
15                 currMaxLen++;
16             }
17             else if(currMin != prevMin){
18                 currMaxLen = 1;
19             }
20             prevMin = currMin;
21             maxLen = Math.max(maxLen, currMaxLen);
22         }
23         return maxLen;
24     }
25 }

 

Solution 3.  O(n) runtime, O(n) space, Using HashSet

Algorithm:  Store all elements into a hash set, thus only unique elements are stored. 

Iterate through all elements in num. For each num[i], go down and up to find the longest

consecutive sequence that has num[i] in it.  In each iteration, delete all the elements

that appear in the current consecutive sequence. Doing this makes this algorithm more

efficient as it will not redudantly consider the same sequence more than once. 

 

 1 public class Solution {
 2     public int longestConsecutive(int[] num) {
 3         if(num == null || num.length == 0){
 4             return 0;
 5         } 
 6         HashSet<Integer> set = new HashSet<Integer>();
 7         for(int i = 0; i < num.length; i++){
 8             set.add(num[i]);
 9         }
10         int longest = 0;
11         for(int i = 0; i < num.length; i++){
12             int down = num[i] - 1;
13             while(set.contains(down)){
14                 set.remove(down);
15                 down--;
16             }
17             int up = num[i] + 1;
18             while(set.contains(up)){
19                 set.remove(up);
20                 up++;
21             }
22             set.remove(num[i]);
23             longest = Math.max(longest, up - down - 1);
24         }
25         return longest;
26     }
27 }

 

 

 

Related Problems 

Binary Tree Longest Consecutive Sequence 

Binary Tree Longest Consecutive Sequence III

Longest Increasing Subsequence 

 

posted @ 2017-07-19 06:08  Review->Improve  阅读(174)  评论(0编辑  收藏  举报