594. Longest Harmonious Subsequence

1. Description

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.

2. Example

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

Example 2:
Input: nums = [1,2,3,4]
Output: 2

Example 3:
Input: nums = [1,1,1,1]
Output: 0

3. Constraints

  • 1 <= nums.length <= $2 * 10^4$
  • $-10^9$ <= nums[i] <= $10^9$

4. Solutions

My Accepted Solution

n = i_nums.size()
Time complexity: O(n)
Space complexity: O(n)

class Solution 
{
public:
    // int findLHS(vector<int>& nums)
    int findLHS(vector<int> &i_nums) 
    {
        map<int, int> numberTimes;
        for(int i = 0; i < i_nums.size(); i++)
            numberTimes[i_nums[i]]++;
        
        int result = 0;
        int lastNumber = (*numberTimes.begin()).first;
        int lastNumberTimes = (*numberTimes.begin()).second;
        
        auto iter = numberTimes.begin();
        for(iter++; iter != numberTimes.end(); iter++)
        {
            if(iter->first == lastNumber + 1)
                result = max(result, iter->second + lastNumberTimes);
            
            lastNumber = iter->first;
            lastNumberTimes = iter->second;
        }
        
        return result;
    }
};

4.1 Sort

n = i_nums.size()
Time complexity: O($nlog_2n$)
Space complexity: O(1)

class Solution 
{
public:
    // int findLHS(vector<int>& nums)
    int findLHS(vector<int> &m_nums) 
    {
        sort(m_nums.begin(), m_nums.end());
        
        int result = 0;
        int currentLeft = 0, nextLeft = 0, nextRight = 0;
        for( ; currentLeft < m_nums.size(); currentLeft = nextLeft)
        {
            nextLeft = currentLeft;
            while(nextLeft < m_nums.size() && m_nums[nextLeft] == m_nums[currentLeft]) 
                nextLeft++; // find the next value
            
            nextRight = nextLeft;
            while(nextRight < m_nums.size() && m_nums[nextRight] == m_nums[nextLeft]) 
                nextRight++;
            
            if(nextLeft < m_nums.size() && nextRight <= m_nums.size() && m_nums[currentLeft] == m_nums[nextLeft] - 1)
                result = max(result, nextRight - currentLeft);
        }
        
        result = max(result, (int)m_nums.size() - currentLeft);
        
        return result;
    }
};

4.2 Hash Table - One Loop

n = i_nums.size()
Time complexity: O(n)
Space complexity: O(n)

class Solution 
{
public:
    // int findLHS(vector<int>& nums)
    int findLHS(vector<int> &i_nums) 
    {
        int result = 0;
        unordered_map<int, int> numberTimes;
        for(int i = 0; i < i_nums.size(); i++)
        {
            numberTimes[i_nums[i]]++;
            
            if(numberTimes[i_nums[i] - 1])
                result = max(result, numberTimes[i_nums[i]] + numberTimes[i_nums[i] - 1]);
            if(numberTimes[i_nums[i] + 1])
                result = max(result, numberTimes[i_nums[i]] + numberTimes[i_nums[i] + 1]);
        }
        
        return result;
    }
};
comments powered by Disqus