当前位置:网站首页>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;
}
边栏推荐
- Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
- Double.doubleToLongBits() method uses
- MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
- 3款不同类型的自媒体免费工具,有效提高创作、运营效率
- 【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
- Gartner reiterates the important value of 'data weaving'
- Memory problems difficult to locate, it is because you do not use ASAN
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- 做自媒体月入几万?博主们都在用的几个自媒体工具
- From the product dimension, why can't we fully trust Layer2?
猜你喜欢
![[Go WebSocket] 多房间的聊天室(一)思考篇](/img/c9/4374a57c6a4ae02f606253a4c299e4.png)
[Go WebSocket] 多房间的聊天室(一)思考篇

Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户

What is an abstract class

内存问题难定位,那是因为你没用ASAN

什么是抽象类
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)

模块九 - 设计电商秒杀系统

Mobile and PC compatible loading and toast message plugins

2023版揽胜运动曝光,安全、舒适一个不落
随机推荐
力扣练习——58 验证二叉搜索树
LeetCode_443_压缩字符串
flask-restplus接口地址404问题
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
力扣练习——62 有效的数独
OneFlow源码解析:算子指令在虚拟机中的执行
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
推荐6个自媒体领域,轻松易上手
POJ 1026 Cipher (Permutation Groups)
快速上手,征服三种不同分布式架构调用方案
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
使用.NET简单实现一个Redis的高性能克隆版(六)
2023版揽胜运动曝光,安全、舒适一个不落
如何使用工程仪器设备在线监测管理系统
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
Some tips for using Unsafe
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
越折腾越好用的 3 款开源 APP
【小程序 | 启航篇】一文打通任督二脉
不止跑路,拯救误操作rm -rf /*的小伙儿