-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Dequeue.java
More file actions
37 lines (34 loc) · 1.09 KB
/
Java Dequeue.java
File metadata and controls
37 lines (34 loc) · 1.09 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);
final Deque<Integer> deque = new ArrayDeque<Integer>();
final Map<Integer, Integer> map = new HashMap<Integer, Integer>();
final int n = in.nextInt();
final int m = in.nextInt();
// java 15
int res = 0;
for (int i = 0; i < n; i++) {
final int num = in.nextInt();
deque.addLast(num);
if (map.containsKey(num)) {
map.put(num, map.get(num).intValue() + 1);
} else {
map.put(num, 1);
}
if (deque.size() == m + 1) {
final int key = deque.removeFirst();
final int v = map.get(key);
if (v == 1) {
map.remove(key);
} else {
map.put(key, v - 1);
}
}
final int cnt = map.size();
if (cnt > res) { res = cnt; }
}
System.out.println(res);
}
}
// Have a nice day.