当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?

mysql5.7 installation and deployment - yum installation

Pycharm终端出现PS问题、conda或activate不是内部命令问题..

推荐6个自媒体领域,轻松易上手

用proteus直接仿真stm32-可以完全丢弃编程器

2022年裁员潮,失业程序员何去何从?

中小规模网站架构

快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?

mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded

第2章-矩阵及其运算-矩阵创建(1)
随机推荐
Double.doubleToLongBits() method uses
【勇敢饭饭,不怕刷题之链表】有序链表的合并
Will SQL and NoSQL eventually converge?
使用.NET简单实现一个Redis的高性能克隆版(六)
程序员追求技术夯实基础学习路线建议
三相380V整流后的电压
关于振弦采集模块及采集仪振弦频率值准确率的问题
【Untitled】
Nocalhost - 让云原生时代的开发更高效
2022年裁员潮,失业程序员何去何从?
推荐6个自媒体领域,轻松易上手
使用哈工大LTP测试分词并且增加自定义字典
力扣练习——63 找到字符串中所有字母异位词
Some tips for using Unsafe
HDU 1520 Anniversary party (tree dp)
The impact of development mode on testing
flask-restplus接口地址404问题
LeetCode_628_三个数的最大乘积
开发模式对测试的影响
老板加薪!看我做的WPF Loading!!!