-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-dequeue.java
More file actions
37 lines (29 loc) · 1 KB
/
java-dequeue.java
File metadata and controls
37 lines (29 loc) · 1 KB
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
31
32
33
34
35
36
37
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<Integer>();
HashMap<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
int n = in.nextInt();
int m = in.nextInt();
int ans = 0, distinct = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.addLast(num);
hashmap.put(num, hashmap.getOrDefault(num, 0) + 1);
if (hashmap.get(num) == 1)
distinct++;
if (deque.size() == m + 1) {
int element = deque.removeFirst();
hashmap.put(element, hashmap.get(element) - 1);
if (hashmap.get(element) == 0)
distinct--;
}
if (deque.size() == m) {
if (distinct > ans)
ans = distinct;
}
}
System.out.println(ans);
}
}