799. 最长连续不重复子序列(双指针板子)

最长连续不重复子序列(双指针板子)

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int a[N], s[N];

signed main(){
    int n; scanf("%d", &n);
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    
    int res = 0;
    for(int i = 1, j = 1; i <= n; ++i){
        s[a[i]]++;
        while(s[a[i]] > 1){
            s[a[j++]]--;
        }
        res = max(res, i - j + 1);
    }
    printf("%d\n", res);
    return 0; 
}
posted @ 2025-04-05 12:29  awei040519  阅读(13)  评论(0)    收藏  举报