-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path124.h
More file actions
35 lines (34 loc) · 1.01 KB
/
124.h
File metadata and controls
35 lines (34 loc) · 1.01 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
class Solution {
public:
/*
* @param num: A list of integers
* @return: An integer
*/
int longestConsecutive(vector<int> &num) {
// write your code here
map<int,int> mp;
int longest = 1;
for(int i = 0; i < num.size(); i++){
auto iter = mp.find(num[i]);
if(iter == mp.end()){
mp.insert(pair<int,int>(num[i], 1));
if(mp.find(num[i] + 1) != mp.end()){
longest = max(longest, back_forward(mp, num[i], num[i]+1));
}
if(mp.find(num[i] - 1) != mp.end()){
longest = max(longest, back_forward(mp, num[i]-1, num[i]));
}
}
}
return longest;
}
int back_forward(map<int,int> &mp, int left, int right){
int forward = left - (mp[left]-1);
int back = right + mp[right]-1;
int len = back - forward + 1;
//update
mp[back] = len;
mp[forward]= len;
return len;
}
};