-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaDeque.java
More file actions
70 lines (59 loc) · 1.84 KB
/
JavaDeque.java
File metadata and controls
70 lines (59 loc) · 1.84 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
public class JavaDeque {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Deque deque = new ArrayDeque<Integer>();
HashSet<Integer> hashSet = new HashSet<>();
int maxunique = 0;
int n = sc.nextInt();
int m = sc.nextInt();
for (int i = 0; i < n; i++) {
int num = sc.nextInt();
deque.addLast(num);
hashSet.add(num);
if(deque.size() == m){
maxunique = Math.max(maxunique, hashSet.size());
// if(maxunique < hashSet.size()){
// maxunique = hashSet.size();
// }
for(Object j: deque){
System.out.print((int)j+" ");
}
System.out.print("\n");
Object out = deque.removeFirst();
if (!deque.contains(out)){
hashSet.remove((Integer)out);
}
}
}
// Error at 3 3 \n 1 2 3
// for (int i = 0; i < n; i++) {
// int num = sc.nextInt();
//
// if(deque.size() == m){
//
// if(i == m){
// hashSet = new HashSet<>(deque);
// maxunique = hashSet.size();
// }
// //firstout
// Object out = deque.pollFirst();
// //lastin
// deque.addLast(num);
//
// if (!deque.contains(out)){
// hashSet.remove((Integer)out);
// }
// hashSet.add(num);
// if(maxunique < hashSet.size()){
// maxunique = hashSet.size();
// }
//
//
// }else{
// deque.add(num);
// }
// }
System.out.println(maxunique);
}
}