-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path594.cpp
More file actions
29 lines (29 loc) · 846 Bytes
/
Copy path594.cpp
File metadata and controls
29 lines (29 loc) · 846 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
class Solution {
public:
int findLHS(vector<int>& nums) {
if(nums.empty()) return 0;
sort(nums.begin(), nums.end());
int first = nums[0];
int second = INT_MIN;
int count_1 = 1;
int count_2 = 0;
int max_val = 0;
for(int i=1; i<nums.size(); i++){
if(nums[i]==first) count_1++;
else if(nums[i]!=first && second==INT_MIN){
count_2++;
second=nums[i];
}
else if(nums[i]!=first && second!=INT_MIN && nums[i]==second) count_2++;
else{
max_val = (second-first==1)?max(max_val, count_1+count_2):max_val;
count_1 = count_2;
count_2 = 1;
first = second;
second = nums[i];
}
}
if(second-first==1) max_val = max(max_val, count_1+count_2);
return max_val;
}
};