-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0039.java
More file actions
43 lines (34 loc) · 931 Bytes
/
HackerrankJava0039.java
File metadata and controls
43 lines (34 loc) · 931 Bytes
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
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class HackerrankJava0039 {
public static void main(String[] args) {
// Java Dequeue
// https://www.hackerrank.com/challenges/java-dequeue/problem?isFullScreen=true
Scanner in = new Scanner(System.in);
Deque deque = new ArrayDeque<>();
int n = in.nextInt();
int m = in.nextInt();
int maxResult = 0;
Set<Integer> setForCount = new HashSet<>();
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.addLast(num);
setForCount.add(num);
if (deque.size() == m) {
int tmpResult = setForCount.size();
if (maxResult < tmpResult) {
maxResult = tmpResult;
}
int removeTmp = (int) deque.removeFirst();
if (!deque.contains(removeTmp)) {
setForCount.remove(removeTmp);
}
}
}
System.out.println(maxResult);
in.close();
}
}