Leetcode 594 Solution

This article provides solution to leetcode question 594 (longest-harmonious-subsequence)

https://leetcode.com/problems/longest-harmonious-subsequence

Solution

class Solution:
    def findLHS(self, nums: List[int]) -> int:
        m = collections.Counter(nums)

        ans = 0
        for k in m:
            if k + 1 not in m:
                continue
            ans = max(ans, m[k] + m[k + 1])
        return ans