当前位置:网站首页>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;
}
边栏推荐
- 老板加薪!看我做的WPF Loading!!!
- Redis设计与实现
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- 金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
- 4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
- 机器学习之暴力调参案例
- flask-restplus接口地址404问题
- Get started quickly and conquer three different distributed architecture calling schemes
- Three-phase 380V rectified voltage
- 从产品维度来看 我们为什么不能完全信任Layer2?
猜你喜欢

GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%

ENVI 5.3软件安装包和安装教程

4 of huawei offer levels, incredibly side is easing the bit in the interview ali?

OneFlow source code parsing: operator instructions executed in a virtual machine

STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create

1-IMU参数解析以及选择

做自媒体月入几万?博主们都在用的几个自媒体工具

开发模式对测试的影响

Research on motion capture system for indoor combined positioning technology

The impact of development mode on testing
随机推荐
Codeforces 862 C. Mahmoud and Ehab and the xor (技巧)
Programmers pursue technology to consolidate basic learning route suggestions
SQL与NoSQL最终会走向融合吗?
rider内Mono脚本找不到引用资源
POJ 1026 Cipher (置换群)
内存问题难定位,那是因为你没用ASAN
使用.NET简单实现一个Redis的高性能克隆版(六)
OneFlow source code parsing: operator instructions executed in a virtual machine
Chapter 22 Source Code File REST API Reference (4)
CodeChef STMRRG String Merging (dp)
力扣练习——59 从二叉搜索树到更大和树
Memory problems difficult to locate, it is because you do not use ASAN
快速上手,征服三种不同分布式架构调用方案
Several small projects that I have open sourced over the years
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
越折腾越好用的 3 款开源 APP
HDU 1520 Anniversary party (tree dp)
【Untitled】
[E-commerce operation] Do you really understand social media marketing (SMM)?
8月份DB-Engines 数据库排行榜最新战况