594. Longest Harmonious Subsequence Easy

@problem@discussion
#Array#Hash Table#Sorting



1/**
2 * [594] Longest Harmonious Subsequence
3 *
4 * We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.
5 * 
6 * Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.
7 * 
8 * 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.
9 * 
10 *  
11 * Example 1:
12 * 
13 * 
14 * Input: nums = [1,3,2,2,5,2,3,7]
15 * Output: 5
16 * Explanation: The longest harmonious subsequence is [3,2,2,2,3].
17 * 
18 * 
19 * Example 2:
20 * 
21 * 
22 * Input: nums = [1,2,3,4]
23 * Output: 2
24 * 
25 * 
26 * Example 3:
27 * 
28 * 
29 * Input: nums = [1,1,1,1]
30 * Output: 0
31 * 
32 * 
33 *  
34 * Constraints:
35 * 
36 * 
37 * 	1 <= nums.length <= 2 * 10^4
38 * 	-10^9 <= nums[i] <= 10^9
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/longest-harmonious-subsequence/
44// discuss: https://leetcode.com/problems/longest-harmonious-subsequence/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn find_lhs(nums: Vec<i32>) -> i32 {
50        
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_594() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.