Date: 2026-06-06
Time: 17:26
This file solves LeetCode 594 — Longest Harmonious Subsequence. A harmonious subsequence is one where the difference between the maximum and minimum values is exactly 1. The file owns the single function findLHS that computes this.
findLHS(nums: List[int]) -> intTakes a list of integers, returns the length of the longest harmonious subsequence, or 0 if none exists.
The key insight: because a harmonious subsequence must have max - min == 1, every element in it must be one of exactly two consecutive values — say k and k+1. The order of elements in the original list doesn't matter for subsequences (you can pick any subset), so the problem reduces to: for each pair of consecutive integers (k, k+1) present in nums, how many total elements have value k or k+1? Take the max.
Frequency counting + adjacency check — a common LeetCode idiom. Rather than examining O(n^2) pairs, it collapses the array into a frequency map and iterates over distinct keys. This is the canonical O(n) approach for this problem.
The iteration only checks k + 1 (not k - 1), which avoids double-counting — every valid pair (k, k+1) is visited exactly once, when iterating over k.
Imports: Counter from collections (does the frequency counting) and List from typing (type annotation only).
Imported by: longest-harmonious-subsequence/test_solution.py — the test file. The massive "Imported By" list in the prompt is noise from the repo's test infrastructure, not actual consumers of this function.
1. Build a frequency map: Counter(nums) — O(n).
2. For each distinct value k in the counter, check if k + 1 also exists.
3. If so, the harmonious subsequence using values {k, k+1} has length count[k] + count[k+1]. Track the max.
4. Return the max (0 if no adjacent pair exists).
k + 1 in count) guarantees each adjacent pair is evaluated once.None. The function assumes valid input per LeetCode constraints. An empty list naturally returns 0 because the for loop doesn't execute.
longest-harmonious-subsequence/test_solution.py — See the edge cases tested (empty list, all-same values, negative numbers)longest-harmonious-subsequence/plan.md — The planning doc may show alternative approaches considered (sorting-based O(n log n), sliding window)degree-of-an-array/solution.py:findShortestSubArray — Another frequency-counting problem with a similar Counter-based pattern but different objectivecounter-based-leetcode-patterns — Many easy/medium problems (majority element, top-k frequent, etc.) share this Counter + single-pass idiomlhs-returns-zero-for-uniform-list — findLHS returns 0 when all elements are identical, because max - min == 0, not 1lhs-linear-time — The algorithm runs in O(n) time and O(n) space via a single Counter construction and one pass over distinct keyslhs-one-directional-check — Only k + 1 is checked (never k - 1), ensuring each valid pair is counted exactly oncelhs-subsequence-not-subarray — The solution correctly treats the input as a subsequence problem (order-independent) by using counts rather than positional logic