Advertisement

Longest Harmonious Subsequence - LeetCode 594 Solution

Longest Harmonious Subsequence - Complete Solution Guide

Longest Harmonious Subsequence is LeetCode problem 594, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. 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 Explanation: The longest harmonious subsequences are [1,2] , [2,3] , and [3,4] , all of w

Detailed Explanation

The problem requires us to find the length of the longest harmonious subsequence within a given integer array `nums`. A harmonious array is defined as an array where the difference between its maximum and minimum values is exactly 1. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. We need to consider all possible subsequences and find the longest one that satisfies the harmonious property. If no harmonious subsequence exists, we should return 0.

Solution Approach

The solution uses a hash map to count the occurrences of each number in the input array. Then, it iterates through the hash map and for each number `num`, it checks if `num + 1` also exists in the hash map. If it does, it calculates the sum of their counts (which represents the length of a harmonious subsequence formed by `num` and `num + 1`) and updates the `max_len` if the current harmonious subsequence length is greater than the current `max_len`. Finally, the algorithm returns `max_len`.

Step-by-Step Algorithm

  1. Step 1: Create an empty hash map (dictionary) `count` to store the frequency of each number in the input array `nums`.
  2. Step 2: Iterate through the `nums` array and update the `count` hash map. For each `num` in `nums`, increment its count in the `count` hash map.
  3. Step 3: Initialize `max_len` to 0. This variable will store the length of the longest harmonious subsequence found so far.
  4. Step 4: Iterate through the `count` hash map. For each `num` in `count`, check if `num + 1` also exists as a key in the `count` hash map.
  5. Step 5: If `num + 1` exists in the `count` hash map, calculate the sum of the counts of `num` and `num + 1` in the `count` hash map.
  6. Step 6: Update `max_len` with the maximum value between the current `max_len` and the sum calculated in step 5.
  7. Step 7: After iterating through all numbers in the `count` hash map, return the final `max_len`.

Key Insights

  • Insight 1: The longest harmonious subsequence must contain elements 'x' and 'x+1' for some value 'x'. We can iterate through all possible values 'x' and find the counts of 'x' and 'x+1' in the array.
  • Insight 2: Using a hash map (or dictionary) is an efficient way to store and retrieve the frequency of each number in the input array.
  • Insight 3: We can find the maximum length by iterating through the keys of the hash map and checking if key+1 exists. The length of the harmonious subsequence will be the sum of counts of the key and key+1.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Sliding Window, Sorting, Counting.

Companies

Asked at: LiveRamp, ZS Associates.