LeetCode - Longest Consecutive Sequence

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 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3517935.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值