-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path594.cpp
More file actions
30 lines (29 loc) · 872 Bytes
/
594.cpp
File metadata and controls
30 lines (29 loc) · 872 Bytes
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
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> counts;
for (auto& num : nums) counts[num]++;
vector<pair<int, int>> v;
for (auto& element : counts) v.push_back({element.first, element.second});
sort(v.begin(), v.end());
int n = v.size();
int res = 0;
for (int i = 0; i < n - 1; ++i) {
if (v[i + 1].first - v[i].first == 1) res = max(res, v[i + 1].second + v[i].second);
}
return res;
}
};
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> cnts;
for (auto& num : nums) cnts[num]++;
int res = 0;
for (auto& [num, cnt] : cnts) {
if (!cnts.count(num + 1)) continue;
res = max(res, cnts[num] + cnts[num + 1]);
}
return res;
}
};