题目描述
blablabla
算法1
时间复杂度
O(n)
Java 代码
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] a = new int[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int res = 0;
for (int i = 0, j = 0; i < n; i++) {
map.put(a[i], map.getOrDefault(a[i], 0) + 1);
while (map.get(a[i]) > 1) {
map.put(a[j], map.get(a[j]) - 1);
j++;
}
res = Math.max(res, i - j + 1);
}
System.out.println(res);
}
}