当前位置:网站首页>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;
}
边栏推荐
- Break through the dimensional barriers and let the dolls around you move on the screen!
- Gartner reiterates the important value of 'data weaving'
- 用proteus直接仿真stm32-可以完全丢弃编程器
- 4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
- Mobile and PC compatible loading and toast message plugins
- 老板加薪!看我做的WPF Loading!!!
- Codeforces 814 C. An impassioned circulation of affection (dp)
- Nocalhost - 让云原生时代的开发更高效
- 阻塞 非阻塞 poll机制 异步
- 一文带你搞懂中断按键驱动程序之poll机制
猜你喜欢
振弦传感器及核心VM系列振弦采集模块
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
第5章相似矩阵及二次型(4)
推荐6个自媒体领域,轻松易上手
A little self-deprecating deconstruction about farmers "code"
从脚本到剪辑,影像大师亲授的后期制作秘籍
如何使用工程仪器设备在线监测管理系统
8月份DB-Engines 数据库排行榜最新战况
机器学习之暴力调参案例
随机推荐
Gartner reiterates the important value of 'data weaving'
Emulate stm32 directly with proteus - the programmer can be completely discarded
Double.doubleToLongBits()方法使用
一文带你搞懂中断按键驱动程序之poll机制
快速上手,征服三种不同分布式架构调用方案
POJ 1026 Cipher (Permutation Groups)
Article take you understand interrupt the key driver of polling mechanism
越折腾越好用的 3 款开源 APP
【机器学习】浅谈正规方程法&梯度下降
The brave rice rice, does not fear the brush list of 】 list has a ring
blocking non-blocking poll mechanism asynchronous
Research on motion capture system for indoor combined positioning technology
关于“码农”的一点自嘲解构
力扣练习——63 找到字符串中所有字母异位词
杭电多校-Loop-(不确定性贪心+线段树)
零基础想自学软件测试,有没有大佬可以分享下接下来的学习书籍和路线?
HCIP ---- VLAN
In August the DB list latest scores - database Engines
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
【Untitled】