(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;
}
相关推荐
蓝色汪洋1 小时前
xtu oj矩阵
算法
hh随便起个名7 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
Dingdangcat869 小时前
城市交通多目标检测系统:YOLO11-MAN-FasterCGLU算法优化与实战应用_3
算法·目标检测·目标跟踪
tang&9 小时前
滑动窗口:双指针的优雅舞步,征服连续区间问题的利器
数据结构·算法·哈希算法·滑动窗口
拼命鼠鼠10 小时前
【算法】矩阵链乘法的动态规划算法
算法·矩阵·动态规划
LYFlied10 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
式51610 小时前
线性代数(八)非齐次方程组的解的结构
线性代数·算法·机器学习
橘颂TA11 小时前
【剑斩OFFER】算法的暴力美学——翻转对
算法·排序算法·结构与算法
叠叠乐11 小时前
robot_state_publisher 参数
java·前端·算法
hweiyu0011 小时前
排序算法:冒泡排序
算法·排序算法