-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJava Dequeue.java
39 lines (33 loc) · 1.09 KB
/
Java Dequeue.java
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
38
39
import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.HashMap;
public class test {
public static void main(String[] args) {
HashMap<Integer, Integer> map = new HashMap();
ArrayDeque<Integer> deque = new ArrayDeque();
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int max = 0;
for (int i = 0; i < n; i++) {
if (i >= m) {
int old = deque.removeFirst();
if (map.get(old) == 1) {
map.remove(old);
} else {
map.merge(old, -1, Integer::sum);
}
}
/* Add new value */
int num = scan.nextInt();
deque.addLast(num);
map.merge(num, 1, Integer::sum);
max = Math.max(max, map.size());
if (max == m) {
break;
}
}
scan.close();
System.out.println(max);
}
}