当前位置:网站首页>Clicking Exercise - 64 Longest Harmonic Subsequences
Clicking Exercise - 64 Longest Harmonic Subsequences
2022-08-10 11:33:00 【qq_43403657】
64 最长和谐子序列
1.问题描述
Harmony refers to an array an array(长度>=2)Elements in the difference between the maximum and the minimum is just1.
现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度.
If you can't find such an array,则输出0.
示例 :
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].
说明: Input array length is more than20,000.
2.输入说明
First of all, the length of the input integer arrayn,2<=n<=10000
然后输入n个整数,以空格分隔.
3.输出说明
The length of the output of the longest harmonious subsequence
4.范例
输入
8
1 3 2 2 5 2 3 7
输出
5
5.代码
#include<iostream>
#include<map>
#include<string>
#include<unordered_map>
#include<algorithm>
#include<string.h>//memset函数
using namespace std;
int maxLen(vector<int> nums)
{
unordered_map<int, int>hash;
int ans = 0;
for (int num : nums)
hash[num]++;
for (int i=0;i<nums.size();i++)
{
if (hash[nums[i]] && hash[nums[i] + 1])//若nums[i]和nums[i]+1都存在
{
ans = max(ans, hash[nums[i]] + hash[nums[i] + 1]);
}
}
return ans;
}
int main()
{
int n,tmp;
cin >> n;
vector<int> nums;
for (int i = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
int res = maxLen(nums);
cout << res << endl;
return 0;
}
边栏推荐
- 使用JMeter进行MySQL的压力测试
- L2 applications from a product perspective: why is it a playground?
- VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
- 关于“码农”的一点自嘲解构
- 3 injured in 'electrical accident' at Google data center
- ENVI 5.3软件安装包和安装教程
- 力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
- 越折腾越好用的 3 款开源 APP
- 力扣练习——63 找到字符串中所有字母异位词
- 微信小程序提交审核历史版本记录从哪里查看
猜你喜欢
随机推荐
阻塞 非阻塞 poll机制 异步
力扣练习——61 根据字符出现频率排序
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
基于UiAutomator2+PageObject模式开展APP自动化测试实战
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
为什么Redis很快
8月份DB-Engines 数据库排行榜最新战况
OneFlow source code parsing: operator instructions executed in a virtual machine
JWT implements login authentication + Token automatic renewal scheme
Will SQL and NoSQL eventually converge?
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
Break through the dimensional barriers and let the dolls around you move on the screen!
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
Codeforces 814 C. An impassioned circulation of affection (dp)
振弦传感器及核心VM系列振弦采集模块
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
Chapter 22 Source Code File REST API Reference (4)
力扣练习——56 寻找右区间
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
blocking non-blocking poll mechanism asynchronous









