Algorithm


Problem Name: 594. Longest Harmonious Subsequence

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.

 

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

 

Constraints:

  • 1 <= nums.length <= 2 * 104
  • -109 <= nums[i] <= 109
 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findLHS(int[] nums) {
    int maxSubarraySize = 0;
    Map < Integer, Integer> map = new HashMap<>();
    for (int num : nums) {
      map.put(num, map.getOrDefault(num, 0) + 1);
      if (map.containsKey(num + 1)) {
        maxSubarraySize = Math.max(maxSubarraySize, map.get(num) + map.get(num + 1));
      }
      if (map.containsKey(num - 1)) {
        maxSubarraySize = Math.max(maxSubarraySize, map.get(num) + map.get(num - 1));
      }
    }
    return maxSubarraySize;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,2,2,5,2,3,7]

Output

x
+
cmd
5

#2 Code Example with Javascript Programming

Code - Javascript Programming


const findLHS = function(nums) {
    if(nums == null) return 0
    if(Object.prototype.toString.call(nums) === '[object Array]' && nums.length === 0) return 0
    let res = 0
    const map = {}
    for (let el of nums) {
        if(map.hasOwnProperty(el)) {
           map[el] +=  1
        } else {
           map[el] = 1
        }
    }
    Object.keys(map).forEach(el => {
        if(map.hasOwnProperty(+el + 1)) {
            res = Math.max(res, map[el] + map[+el + 1])
        }
    })
    console.log(res)
    return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,2,2,5,2,3,7]

Output

x
+
cmd
5

#3 Code Example with C# Programming

Code - C# Programming


using System;
using System.Collections.Generic;

namespace LeetCode
{
    public class _0594_LongestHarmoniousSubsequence
    {
        public int FindLHS(int[] nums)
        {
            var counts = new Dictionary < int, int>();

            var result = 0;
            foreach (var num in nums)
            {
                if (!counts.ContainsKey(num))
                    counts[num] = 1;
                else
                    counts[num]++;
            }

            foreach (var key in counts.Keys)
                if (counts.ContainsKey(key + 1))
                    result = Math.Max(result, counts[key] + counts[key + 1]);

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,4]

Output

x
+
cmd
2
Advertisements

Demonstration


Previous
#593 Leetcode Valid Square Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#596 Leetcode Classes More Than 5 Students Solution in SQL Leetcode