当前位置:网站首页>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));
}
}
边栏推荐
- Concurrent programming -- what is threading?
- Service fusing and degradation of Note Series
- 2021.9.30学习日志-postman
- Timeout thread log for tongweb
- Unity游戏优化(第2版)学习记录7
- A fast week
- Pychart professional edition's solution to SQL script error reporting
- Vagrant virtual machine installation, disk expansion and LAN access tutorial
- = = relation between int and integer
- Exception after repeated application redeployment on tongweb: application instance has been stopped already or outofmemoryerror:metaspace
猜你喜欢

Mongodb Multi - field Aggregation group by

零拷贝技术

NVIDIA Jetson Nano/Xavier NX 扩容教程

One of PowerShell optimizations: prompt beautification

Comment procéder à l'évaluation des algorithmes

Explanation of sentinel series' features, composition and deployment

Building a stand-alone version of Nacos series

JS output uincode code

17 servicetask of flowable task
![[China & some provinces and cities] JSON file for offline map visualization](/img/ea/0c552e1e133f693b9902c54c2e09c8.jpg)
[China & some provinces and cities] JSON file for offline map visualization
随机推荐
Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
Initial redis experience
How to Algorithm Evaluation Methods
Four shardingsphere JDBC sharding strategies
Sentinel series introduction to service flow restriction
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
OpenGL馬賽克(八)
Vagrant virtual machine installation, disk expansion and LAN access tutorial
2021.9.29学习日志-Restful架构
MongoDB 多字段聚合Group by
Building a stand-alone version of Nacos series
@Detailed explanation of propertysource usage method and operation principle mechanism
Getclassloader() returns null, getclassloader() gets null
Shardingsphere JDBC < bind table > avoid join Cartesian product
9. Errorstartevent and errorboundaryevent of error events
19 calling subprocess (callactivity) of a flowable task
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
Bicolor case
Input a number and output each digit from high to low
Sentinel series integrates Nacos and realizes dynamic flow control