forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longestConsecutiveSequence.cpp
65 lines (58 loc) · 1.92 KB
/
longestConsecutiveSequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Source : https://oj.leetcode.com/problems/longest-consecutive-sequence/
// Author : Hao Chen
// Date : 2014-06-22
/**********************************************************************************
*
* 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.
*
*
**********************************************************************************/
//
// Obviously, the easist way is sort the array, however the run-time complexity is O(nlogn)
//
// If we cannot use the sort algorithm, then it seems we have to use O(n^2) solution.
//
// That's fine, let's take a look the O(n^2) soultion
//
// 1) for each item num[i] in the array
// 2) for loop to seach ...... num[i-2], num[i-1], num[i]+1, num[i]+2 ......
//
// We can see, the search is really heavy, and the best data structure for seaching is HashMap.
// hash map is O(1) run-time complexity for seaching.
//
// So, we can have the following solution by using Hash Map.
//
class Solution {
public:
int longestConsecutive(vector<int> &num) {
map<int, int> m;
for (int i=0; i<num.size(); i++){
m[num[i]]=i;
}
int max_seq=0;
for (int i=0; i<num.size(); i++){
int cnt=1;
for(int n = num[i]+1;m.find(n)!=m.end();n++){
m.erase(m.find(n));
cnt++;
}
for(int n = num[i]-1;m.find(n)!=m.end();n--){
m.erase(m.find(n));
cnt++;
}
if (max_seq < cnt){
max_seq = cnt;
}
if (m.size()==0){
break;
}
}
return max_seq;
}
};