当前位置:网站首页>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;
}
};边栏推荐
- How to use regex in file find
- Week 10 - task 1- fill in the blank: line class inherits point class
- Longest substring between two identical characters of leetcode simple question
- Establishing the development environment of esp8266
- The echares map is implemented separately by provinces, and the tooltip user-defined prompt box, scattered annotation and scattered illumination are explained in detail
- 分享 10 个 JS Promise 相关的面试题
- Hyperledger Fabric 2. X custom smart contract
- Easy to understand TCP four waves (multi picture explanation)
- Design of leetcode simple problem goal parser
- 力扣今日题-324. 摆动排序 II
猜你喜欢

用机器人教育创造新一代生产和服务工具

Linux Installation redis

二叉树的迭代法前序遍历的两种方法

Single application and microservice application

作为一名合格的网工,你必须掌握的 DHCP Snooping 知识!

Test Development - ten years of sharpening one sword (VII) interface test tool postman

2022.02.15 - 240. Lucky number in matrix

Internet enterprises need CRM software to help

融入STEAM教育的劳动技能课程

Two houses with different colors and the farthest distance
随机推荐
Case of single file component files
VerilogA——计数器
Fault: display Storport driver out of date in component health
Servlet version conflict causes page 404
JDBC | Chapter 5: closing and releasing JDBC connection resources
Rich material libraries make modeling easy and efficient for developers
Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- length and angle measurement function
How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)
力扣每日一题-第30天-594.最长和谐子序列
Design of leetcode simple problem goal parser
Week 10 - task 3- from point to circle to cylinder
Part 63 - interpreter and compiler adaptation (II)
Where is the Gcov symbol- Where are the gcov symbols?
Pointer from beginner to advanced (2)
Clickhouse data type
Flutter is configured with a domestic image and connected to the real machine
Go basic data types: characters and strings
层次分析法
Overlay histogram with density curve
Venn diagram proportional and color shading with semi transparency