-
Notifications
You must be signed in to change notification settings - Fork 0
/
594.hpp
40 lines (33 loc) · 853 Bytes
/
594.hpp
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
//
// Created by bai on 17-6-26.
//
#ifndef LEETCODE_594_HPP
#define LEETCODE_594_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
class Solution {
public:
int findLHS(vector<int> &nums) {
unordered_map<int, int> nCnt;
for (int n : nums) {
nCnt[n]++;
}
if (nCnt.size() <= 1) {
return 0;
}
int ans = 0;
for_each(nCnt.begin(), nCnt.end(), [&ans, &nCnt](pair<const int &, int> p) {
if (nCnt.count(p.first + 1) > 0) {
ans = max(ans, p.second + nCnt[p.first + 1]);
} else if (nCnt.count(p.first - 1) > 0) {
ans = max(ans, p.second + nCnt[p.first - 1]);
}
});
return ans;
}
};
#endif //LEETCODE_594_HPP