LeetCode 594 Longest Harmonious Subsequence (Python)

Posted by 小明MaxMing on February 4, 2021

题目

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 its longest harmonious subsequence among all its possible subsequences.

A subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.

解题思路

统计每个数出现的次数,求连续两个数出现的最大次数

代码

class Solution:
    def findLHS(self, nums: List[int]) -> int:
        c = Counter(nums)
        res = 0
        for x in c:
            if x + 1 in c:
                res = max(res, c[x] + c[x + 1])
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅