Longest Harmonious Subsequence

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.

Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

Example 1:

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

Note: The length of the input array will not exceed 20,000.

 1 public class Solution {
 2     public int findLHS(int[] nums) {
 3         HashMap<Integer, Integer> numCount = new HashMap<Integer, Integer>();
 4         for (int num : nums) {
 5             numCount.put(num, numCount.getOrDefault(num, 0) + 1);
 6         }
 7
 8         int maxLen = 0;
 9         Set<Integer> keys = numCount.keySet();
10         for (int key : keys) {
11             if (numCount.containsKey(key + 1)) {
12                 maxLen = Math.max(maxLen, numCount.get(key + 1) + numCount.get(key));
13             }
14         }
15
16         return maxLen;
17     }
18 }
时间: 2024-10-10 05:19:55

Longest Harmonious Subsequence的相关文章

[LeetCode] Longest Harmonious Subsequence 最长和谐子序列

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subseque

594. Longest Harmonious Subsequence 最长的和谐子序列

Total Accepted: 5540 Total Submissions: 14066 Difficulty: Easy Contributors:love_Fawn We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to

594. Longest Harmonious Subsequence

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subseque

[LeetCode] 594. Longest Harmonious Subsequence

https://leetcode.com/problems/longest-harmonious-subsequence public class Solution { public int findLHS(int[] nums) { int result = 0; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { map.p

LeetCode算法题-Longest Harmonious Subsequence(Java实现)

这是悦乐书的第270次更新,第284篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第136题(顺位题号是594).我们定义一个和谐数组是一个数组,其最大值和最小值之间的差值恰好为1.给定一个整数数组,在其所有可能的子序列中找到其最长的和谐子序列的长度.例如: 输入:[1,3,2,2,5,2,3,7] 输出:5 说明:最长的和谐子序列是[3,2,2,2,3]. 注意:输入数组的长度不会超过20,000. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there