-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsolve.cpp
More file actions
54 lines (54 loc) · 1.12 KB
/
Copy pathsolve.cpp
File metadata and controls
54 lines (54 loc) · 1.12 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
44
45
46
47
48
49
50
51
52
53
54
#include <unordered_map>
#include <vector>
#include <string>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;
class Solution {
public:
int longestConsecutive(vector<int> &nums) {
unordered_map<int, int> map;
int res = 0;
for (int i : nums) {
if (map.find(i) != map.end()) {
continue;
}
int left = getOrElse(map, i - 1);
int right = getOrElse(map, i + 1);
int sum = left + right + 1;
res = max(res, sum);
map[i] = sum;
map[i - left] = sum;
map[i + right] = sum;
}
return res;
}
private:
int getOrElse(unordered_map<int, int> &map, int key, int defaultValue) {
if (map.find(key) != map.end()) {
return map[key];
} else
return defaultValue;
}
int getOrElse(unordered_map<int, int> &map, int key) {
return getOrElse(map, key, 0);
}
};
int main(int argc, char **argv)
{
Solution solution;
int n;
while (scanf("%d", &n) != EOF) {
vector<int> nums;
for (int i = 0; i < n; ++i) {
int v;
scanf("%d", &v);
nums.push_back(v);
}
printf("%d\n", solution.longestConsecutive(nums));
break;
}
return 0;
}