-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Consecutive_Sequence.cpp
More file actions
43 lines (38 loc) · 1.36 KB
/
Longest_Consecutive_Sequence.cpp
File metadata and controls
43 lines (38 loc) · 1.36 KB
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
/*
We can't not sort for the O(n) complexity. so, the trade off between space and time.
We use hash map. everytime a new integer come to us, we compare it with its neighbor in the hash map.
then we update the max length. we always use the max sub length.
*/
//O(log(N)*N)
class Solution {
public:
int longestConsecutive(vector<int> &num)
{
if (num.empty()) return 0;
unordered_map<int, int> table;
int res = 1;
for (size_t i = 0; i < num.size(); i++)
{
if (table.find(num[i]) != table.end()) continue;
table[num[i]] = 1;
if (table.find(num[i]-1) != table.end())
{
res = max(res, merge(table, num[i]-1, num[i]));
}
if (table.find(num[i]+1) != table.end())
{
res = max(res, merge(table, num[i], num[i]+1));
}
}
return res;
}
int merge(unordered_map<int, int> & table, int left, int right)
{
int lower = left - table[left] + 1;
int upper = right + table[right] - 1;
int len = upper - lower + 1;
table[lower] = len;
table[upper] = len;
return len;
}
};