leetcode 594. Longest Harmonious Subsequence

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.

Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

Example 1:

Input: [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].

Note: The length of the input array will not exceed 20,000.

 

本质上是一个组合问题,C(n,2),但是排序可以降低下复杂度:

class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # brute force
        cnt = collections.Counter(nums)
        items = cnt.items()
        items.sort(key=lambda x:x[0])
        ans = 0        
        for i in xrange(1, len(items)):
            k,v = items[i-1]:
            k2,v2 = items[i]
            if k2-k == 1:
                ans = max(ans, v2+v)
        return ans                        

还可以简化下:

class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # brute force
        cnt = collections.Counter(nums)
        ans = 0
        for k in cnt:
            if k+1 in cnt:
                ans = max(ans, cnt[k+1]+cnt[k])
        return ans

 此外,可以使用排序,本质上是自己build一个hash:

class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # brute force
        nums.sort()
        pre_k = pre_cnt = None
        ans = 0
        i = 0
        while i < len(nums):
            k = nums[i]
            cnt = 1
            while i+1<len(nums) and nums[i+1] == k:
                i += 1
                cnt += 1
            if pre_k is not None:           
                if k-pre_k==1: ans = max(pre_cnt+cnt, ans)                 
            pre_k = k
            pre_cnt = cnt            
            i += 1
        return ans     
        

 

其他解法:

 int findLHS(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int len = 0;
        for(int i = 1, start = 0, new_start = 0; i<nums.size(); i++)
        {

            if (nums[i] - nums[start] > 1)    
                start = new_start;
            if (nums[i] != nums[i-1]) 
                new_start = i;
            if(nums[i] - nums[start] == 1)
                len = max(len, i-start+1);
        }
        return len;

 

 

另外,如果是连续的子序列,则本质上是一个计数问题:

class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        # if sub seq is continueous
        if not nums: return 0
        min_val = max_val = nums[0]
        ans = cnt = 1
        for i in xrange(1, len(nums)):
            max_val = max(nums[i], max_val)
            min_val = min(nums[i], min_val)
            if max_val - min_val <= 1:                
                cnt += 1
                ans = max(ans, cnt)
            else:
                min_val = max_val = nums[i]
                cnt = 1
        return ans                

 

转载于:https://www.cnblogs.com/bonelee/p/8849200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值