-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (26 loc) · 903 Bytes
/
Solution.java
File metadata and controls
30 lines (26 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Solution {
private static final ArrayDeque<Integer> deque = new ArrayDeque<>();
private static final Set<Integer> set = new HashSet<>();
public static void main(String[] args) {
int max = Integer.MIN_VALUE;
try (Scanner sc = new Scanner(System.in)) {
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
deque.add(num);
set.add(num);
if (deque.size() == m) {
max = Math.max(max, set.size());
int first = deque.remove();
if (!deque.contains(first)) set.remove(first);
}
}
}
System.out.println(max);
}
}