Longest Consecutive Sequence

2014.1.13 19:00

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

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

Your algorithm should run in O(n) complexity.

Solution:

  I've seen this problem at the interview of NetEase before. Back then I discussed about an O(n * log(n)) algorithm wit my friends.

  The solution uses a map to record the occurrence of each number. For a number N in the sequence, we scan the map to see if N - 1, N -2, .... and N + 1, N + 2, ... are in the sequence. From that we know the length of the longest consecutive sequence contain N.

  To avoid repeated searches in the map, we'll use another map to mark each element as "already scanned" when they're found in the map.

  For instance, the sequence [3, 1, 2] will produce result 3. It doesn't you start searching from 1, 2 or 3, because you always get [1, 2, 3] at last.

  Like the following:

    1: [] 1 [2, 3]

    2: [1] 2 [3]

    3: [1, 2] 3

  Thus we can mark all consecutive elements as "already scanned" to avoid searching them again in the map. That saves some time indeed.

  Although stl map is quite elegant and optimized, it is still O(log(n)) for every search action. So I chose unordered_map from C++11, which has an ammortized O(1) time complexity for insert and search action.

  It is the difference between a hash-table and a red-black tree that decides their difference in time complexity level.

  Time and space complexity are both O(n).

Accepted code:

 1 // 1AC, excellent!!!
 2 #include <unordered_map>
 3 using namespace std;
 4 
 5 class Solution {
 6 public:
 7     int longestConsecutive(vector<int> &num) {
 8         // IMPORTANT: Please reset any member data you declared, as
 9         // the same Solution instance will be reused for each test case.
10         int res, max_res;
11         unordered_map<int, int> a, b;
12         
13         a.clear();
14         b.clear();
15         
16         int n = num.size();
17         if(n <= 0){
18             return 0;
19         }
20         
21         int i, j;
22         for(i = 0; i < n; ++i){
23             a[num[i]] = 1;
24         }
25         
26         max_res = 0;
27         for(i = 0; i < n; ++i){
28             res = 0;
29             if(b.find(num[i]) != b.end()){
30                 continue;
31             }
32             b[num[i]] = 1;
33             ++res;
34             j = num[i] - 1;
35             while(a.find(j) != a.end() && b.find(j) == b.end()){
36                 b[j] = 1;
37                 ++res;
38                 --j;
39             }
40             j = num[i] + 1;
41             while(a.find(j) != a.end() && b.find(j) == b.end()){
42                 b[j] = 1;
43                 ++res;
44                 ++j;
45             }
46             
47             if(res > max_res){
48                 max_res = res;
49             }
50         }
51         
52         a.clear();
53         b.clear();
54         
55         return max_res;
56     }
57 };

 

 posted on 2014-01-13 19:19  zhuli19901106  阅读(221)  评论(0编辑  收藏  举报