-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathJavaDequeue.java
32 lines (31 loc) · 979 Bytes
/
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
import java.util.*;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque deque = new ArrayDeque<Integer>();
int n = in.nextInt();
int m = in.nextInt();
int a[]=new int[10000001];
int max=0,u=0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
if(deque.size()<m){
deque.addLast(num);
if(a[num]++==0)
u++;
}
else{
if(u>max)
max=u;
if(a[(int)deque.removeFirst()]--==1)
u--;
deque.addLast(num);
if(a[num]++==0)
u++;
}
}
if(u>max)
max=u;
System.out.println(max);
}
}