594 Longest Harmonious Subsequence
Input:
[1,3,2,2,5,2,3,7]
Output:
5
Explanation:
The longest harmonious subsequence is [3,2,2,2,3].from collections import Counter
class Solution:
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hist = Counter(nums)
maxx = 0
for key, _ in hist.items():
if key + 1 in hist:
maxx = max(maxx, hist[key] + hist[key + 1])
return maxxLast updated