当前位置:网站首页>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;
}
};边栏推荐
- QT (x): packaging and deployment
- Analysis comp122 the Caesar cipher
- The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
- 作为一名合格的网工,你必须掌握的 DHCP Snooping 知识!
- Sourcetree remote red exclamation point
- SCM engineering experience - time slice
- P5 DS - component and document Association
- 关于 localStorage 的一些高阶用法
- Failure: unable to log in to "taxpayer equity platform"
- Two houses with different colors and the farthest distance
猜你喜欢

Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- path width style function

Yyds dry goods inventory meituan's two-sided experience, and finally there was a surprise?

Design of leetcode simple problem goal parser

National Defense University project summary

2022.02.15 - SX10-31. House raiding III

Part 63 - interpreter and compiler adaptation (II)

Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?

The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail

Easy to understand TCP four waves (multi picture explanation)

二叉树的迭代法前序遍历的两种方法
随机推荐
Servlet version conflict causes page 404
C language pointer to function
Fresnel diffraction with rectangular aperture based on MATLAB
RedisTemplate处理hash整数类型的问题解析
Where is the Gcov symbol- Where are the gcov symbols?
Fault: ntfrs warning log for id13562
Mongodb sort function
What are the uses of wireless pressure collectors?
What are the uses of final?
Week 12 - task 2- shoulder to shoulder cadres
百度小程序自动提交搜索
JIRA basic usage sharing
力扣今日题-324. 摆动排序 II
Analysis comp122 the Caesar cipher
How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)
Segment in Lucene
The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
AIRNET notes 1
UVM验证平台
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- path width style function