forked from alexprut/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
48 lines (42 loc) · 1.41 KB
/
Solution.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
40
41
42
43
44
45
46
47
48
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<Integer>();
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
int n = in.nextInt();
int m = in.nextInt();
int counter = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
if (i < m) {
deque.addLast(num);
if (hash.get(num) != null) {
hash.put(num, hash.get(num) + 1);
} else {
hash.put(num, 1);
}
} else {
if (counter < hash.size()) { counter = hash.size(); }
int first = deque.removeFirst();
deque.addLast(num);
if (hash.containsKey(first)) {
if (hash.get(first) > 1) {
hash.put(first, hash.get(first) - 1);
} else {
hash.remove(first);
}
}
if (hash.containsKey(num)) {
hash.put(num, hash.get(num) + 1);
} else {
hash.put(num, 1);
}
}
}
if (counter < hash.size()) {
counter = hash.size();
}
System.out.println(counter);
}
}