当前位置:网站首页>Li Kou daily question - day 30 -594 Longest harmonic subsequence
Li Kou daily question - day 30 -594 Longest harmonic subsequence
2022-06-29 06:37:00 【Chongyou research Sen】
2022.6.28 Did you brush the questions today ?
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 .
analysis :
Give you an array , Find the inside that can form ,|x-y|=1 When the conditions ,x and y The maximum number of elements added up .
Ideas : We put each number in a hash table . Then judge whether the key values in the hash table have a difference of 1 Of , If there is , Then find the key value pairs corresponding to the two key values , Then find the largest sum .
analysis :
1. Hashtable
class Solution {
public:
int findLHS(vector<int>& nums) {
unordered_map<int, int> cnt;
int res = 0;
for (int num : nums) {
cnt[num]++;
}
for (auto it : cnt)
{
if (cnt.count(it.first + 1))
{
//it.second Current second
//cnt[it.first + 1] The next one second
res = max(res, it.second + cnt[it.first + 1]);
}
}
return res;
}
};2. Double pointer
We can use two pointers , A number that points to a small number , A number that points to a large number , If the difference between two pointers is not 1, Then put the front , Back pointer keeps moving back , When the satisfaction difference is found, it is 1 when , Then record the subscript difference at this time , Then the back pointer continues to move back , Determine whether the next number meets the .
class Solution {
public:
int findLHS(vector<int>& nums) {
sort(nums.begin(), nums.end());
int begin = 0;
int end = 0;
int res = 0;
for (auto end = 0; end < nums.size(); end++)
{
while (nums[end] - nums[begin] > 1)
{
begin++;
}
if (nums[end] - nums[begin] == 1)
{
res = max(res, end - begin + 1);
}
}
return res;
}
};边栏推荐
- What are the uses of static?
- Antlr4 recognizes the format of escape string containing quotation marks
- Clickhouse data type
- Hyperledger Fabric 2. X custom smart contract
- [deep learning] - maze task learning I (to realize the random movement of agents)
- Delete tag
- Observer mode vs publish subscribe mode
- Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
- Can I cast int to a variable of type byte? What happens if the value is larger than the range of byte type?
- Difference between URI and URL
猜你喜欢

Hyperledger Fabric 2. X custom smart contract

力扣今日题-324. 摆动排序 II

Chapter V online logic analyzer signaltap

MySQL add / delete / modify query SQL statement exercise yyds dry goods inventory

Introduction to Ceres Quartet
![[C language series] - branch and loop statements](/img/bf/656c9189b4ab4387c5acab1c4a2804.jpg)
[C language series] - branch and loop statements

RedisTemplate处理hash整数类型的问题解析

Single application and microservice application

百度小程序自动提交搜索

Pytest (7) -yield and termination function
随机推荐
Clickhouse data type
Delete tag
Presto-Trial
关于DDNS
Creation of Arduino uno development environment
Week 10 - task 3- from point to circle to cylinder
多线程工具类 CompletableFuture
Single application and microservice application
Go compile source code (window environment)
Hyperledger Fabric 2. X custom smart contract
Fault: KDC warning log for id29
Client and server working modes of JVM
JDBC | Chapter 5: closing and releasing JDBC connection resources
P5 DS - component and document Association
Maximum ascending subarray sum of leetcode simple problem
Leetcode simple question: judging the color of a grid on a chess board
关于 localStorage 的一些高阶用法
What are the uses of final?
Mongodb paging method
Call the computer calculator and use it to convert several base numbers