zoukankan      html  css  js  c++  java
  • 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                
    
  • 相关阅读:
    [转载]写作经验谈--如何写一本书?
    决定写一本书
    c中自定义函数通过sizeof来输出数组的长度为何不正确?【原创】
    [转]关于PHP的漏洞以及如何防止PHP漏洞?
    [转]PHP安全之防止你的源代码或重要配置信息暴露在外
    PHPUnit 单元测试框架(鸡肋)
    [转]避免PHP-FPM内存泄漏导致内存耗尽
    [转]PHP ffmpeg截取视频指定帧为图片,获取rotation信息并旋转
    ThinkPHP 缓存 以及Zend OPCache提升PHP性能
    简单测漏 语句
  • 原文地址:https://www.cnblogs.com/bonelee/p/8849200.html
Copyright © 2011-2022 走看看