> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Harmonious Subsequence Python Solution

> Tested Python solution for LeetCode 594 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 594, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Sliding Window](/catalog/topics/sliding-window), [Sorting](/catalog/topics/sorting), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/longest-harmonious-subsequence/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 594   # by problem number
lcpy gen -s longest_harmonious_subsequence   # by problem name
```

## Problem

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.

### Examples

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

```
Input: nums = [1,2,3,4]
Output: 2
Explanation: The longest harmonious subsequences are [1,2], [2,3], and [3,4], all of which have a length of 2.
```

```
Input: nums = [1,1,1,1]
Output: 0
Explanation: No harmonious subsequence exists.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_harmonious_subsequence/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_harmonious_subsequence/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter


class Solution:
    # Time: O(n)
    # Space: O(n)
    def find_lhs(self, nums: list[int]) -> int:
        counts = Counter(nums)
        longest = 0
        for value, count in counts.items():
            if value + 1 in counts:
                longest = max(longest, count + counts[value + 1])
        return longest
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.