aboutsummaryrefslogtreecommitdiff
path: root/longest_harmonious_subseq.py
blob: 125209076bafc9defba9b886d02b1a47d35207a9 (plain)
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
# 594. Longest Harmonious Subsequence
from collections import Counter

"""
we define a harmonious array as an array where the difference between its
maximum value and its minimum value is exactly 1. given an integer array
'nums', return the length of the its longest harmonious subsequnce among all
its possible subsequnces.
"""


class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        c = Counter(nums)
        return max((c[i] + c[i + 1]) * (c[i + 1] != 0) for i in c)


if __name__ == "__main__":
    obj = Solution()
    print(obj.findLHS(nums=[1, 3, 2, 2, 5, 2, 3, 7]))
    print(obj.findLHS(nums=[1, 2, 3, 4]))
    print(obj.findLHS(nums=[1, 1, 1, 1]))