-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaDequeue.java
36 lines (31 loc) · 1 KB
/
JavaDequeue.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
package DataStructure;
import java.util.*;
public class JavaDequeue {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<>();
int n = in.nextInt();
int m = in.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.add(num);
map.put(num, map.get(num) == null ? 1 : map.get(num) + 1);
if (i >= m - 1) {
int size = map.size();
if (max < size) {
max = size;
}
int removed = deque.removeFirst();
int count = map.get(removed);
if (count > 1) {
map.put(removed, count - 1);
} else {
map.remove(removed);
}
}
}
System.out.println(max);
}
}