(AC)Playlist

题目描述

You are given a playlist of a radio station since its establishment. The playlist has a total of n songs.

What is the longest sequence of successive songs where each song is unique?

输入

The first input line contains an integer n(1 ≤ n ≤ 2*105): the number of songs.

The next line has n integers k1,k2,...,kn(1 ≤ ki ≤ 109): the id number of each song.

输出

Print the length of the longest sequence of unique songs.

样例输入 Copy
复制代码
8
1 2 1 3 2 7 4 2
样例输出 Copy
复制代码
5

代码

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
int n,k[200009];
int main(){
	cin>>n;
	for(int i=0;i<n;++i)cin>>k[i];
	map<int,int>book;
	int ans=0,l=0,r=-1;
	for(int i=0;i<n;++i){
		if(book[k[i]]){
			ans=ans>(r-l+1)?ans:r-l+1;
			while(book[k[i]]&&l<=r){
				book[k[l]]--;
				l++;
			}
		}
		r++;
		book[k[i]]++;
	}
	ans=ans>(r-l+1)?ans:r-l+1;
	cout<<ans;
	return 0;
}
相关推荐
夏鹏今天学习了吗13 分钟前
【LeetCode热题100(35/100)】LRU 缓存
算法·leetcode·缓存
拾光Ծ35 分钟前
【C++】STL有序关联容器的双生花:set/multiset 和 map/multimap 使用指南
数据结构·c++·算法
西望云天1 小时前
The 2023 ICPC Asia Shenyang Regional Contest(2023沈阳区域赛CEJK)
数据结构·算法·icpc
zh_xuan2 小时前
LeeCode92. 反转链表II
数据结构·算法·链表·leecode
2401_841495642 小时前
【数据结构】汉诺塔问题
java·数据结构·c++·python·算法·递归·
Q741_1473 小时前
C++ 位运算 高频面试考点 力扣137. 只出现一次的数字 II 题解 每日一题
c++·算法·leetcode·面试·位运算
天特肿瘤电场研究所3 小时前
专业的肿瘤电场疗法厂家
算法
DASXSDW3 小时前
NET性能优化-使用RecyclableBuffer取代RecyclableMemoryStream
java·算法·性能优化
kfepiza3 小时前
CAS (Compare and Swap) 笔记251007
java·算法
墨染点香4 小时前
LeetCode 刷题【103. 二叉树的锯齿形层序遍历、104. 二叉树的最大深度、105. 从前序与中序遍历序列构造二叉树】
算法·leetcode·职场和发展