当前位置:网站首页>Leetcode longest harmonic subsequence simple
Leetcode longest harmonic subsequence simple
2022-06-13 05:50:00 【AnWenRen】
title : The longest harmonious subsequence - Simple
subject
A harmonious array is the difference between the maximum and minimum values of elements in an array Is precisely 1 .
Now? , Give you an array of integers nums , Please find the length of the longest harmonious subsequence among all possible subsequences .
A subsequence of an array is a sequence derived from an array , It can be done by deleting some elements or not deleting elements 、 Without changing the order of the other elements .
Example 1
Input :nums = [1,3,2,2,5,2,3,7]
Output :5
explain : The longest harmonic subsequence is [3,2,2,2,3]
Example 2
Input :nums = [1,2,3,4]
Output :2
Example 3
Input :nums = [1,1,1,1]
Output :0
Tips
1 <= nums.length <= 2 * 104-109 <= nums[i] <= 109
Code Java
public class FindLHS {
// Sorting solution
public int findLHS(int[] nums) {
Arrays.sort(nums);
int ans = 0;
int n = nums.length;
for (int i = 0, j = 0; j < n; j++) {
while (i < j && nums[j] - nums[i] > 1) i++;
if (nums[j] - nums[i] == 1)
ans = Math.max(ans, j - i + 1);
}
return ans;
}
// map solve
public int findLHS1(int[] nums) {
int ans = 0;
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
Set<Integer> set = map.keySet();
for (Integer key : set) {
if (map.containsKey(key + 1)) {
ans = Math.max(ans, map.get(key) + map.get(key + 1));
}
}
return ans;
}
public static void main(String[] args) {
int[] nums = {
1,3,2,2,5,2,3,7};
System.out.println(new FindLHS().findLHS1(nums));
}
}
边栏推荐
- 19 calling subprocess (callactivity) of a flowable task
- Summary of the 11th week of sophomore year
- AUTOSAR实战教程pdf版
- How to Algorithm Evaluation Methods
- Cross compile HelloWorld with cmake
- Function and application scenario of field setaccessible() method
- Integration of sentinel series Nacos to realize rule synchronization and persistence
- Leetcode- keyboard line - simple
- 若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
- Randomly fetch data from the list
猜你喜欢

Concurrent programming -- what is threading?

Nacos series registry principle and source code analysis

890. Find and Replace Pattern

Source code analysis of ArrayList

20 flowable container (event sub process, things, sub process, pool and pool)

How to Algorithm Evaluation Methods

How to view tongweb logs correctly?

Zero copy technology

A simple recursion problem of linked list

powershell优化之一:提示符美化
随机推荐
Concurrent programming -- what is threading?
Leetcode- third largest number - simple
使用cmake交叉编译helloworld
How MySQL optimizes the use of joint index ABC
About the solution of pychart that cannot be opened by double clicking
Source code analysis of ArrayList
设置自定义dialog的正确宽高
Comment procéder à l'évaluation des algorithmes
Initial redis experience
17 servicetask of flowable task
2 first experience of drools
Leetcode- distribute cookies - simple
2021-9-19
MySQL fuzzy query and sorting by matching degree
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Hump naming and underlining
JNDI configuration for tongweb7
AUTOSAR actual combat tutorial pdf version
20 flowable container (event sub process, things, sub process, pool and pool)
Misunderstanding of tongweb due to ease of use