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                
    
  • 相关阅读:
    Spring2.5 注解 Aspect AOP
    Java反射机制深入研究
    Spring声明式事务管理、Spring整合Hibernate
    ettercap 修改页面内容
    asp复制到word实现分页效果
    利用SSLStrip & Ettercap ARP欺骗嗅探密码
    Backtrack下密码字典生成器使用参数(crunch3.0)
    浅析浏览器引擎对JQuery DOM加载和执行
    [置顶] 手机访问统计的技术分析
    Word2003取消首字母大写方法
  • 原文地址:https://www.cnblogs.com/bonelee/p/8849200.html
Copyright © 2011-2022 走看看