-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNineteen.java
More file actions
44 lines (32 loc) · 1.13 KB
/
Nineteen.java
File metadata and controls
44 lines (32 loc) · 1.13 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
package HackerRank;
import java.util.*;
public class Nineteen {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // number of integers
int m = in.nextInt(); // size of subarray
Deque<Integer> deque = new ArrayDeque<Integer>();
Map<Integer, Integer> freqMap = new HashMap<Integer, Integer>();
int maxUnique = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.addLast(num);
if (freqMap.containsKey(num)) {
freqMap.put(num, freqMap.get(num) + 1);
} else {
freqMap.put(num, 1);
}
if (deque.size() == m) {
maxUnique = Math.max(maxUnique, freqMap.size());
int removed = deque.removeFirst();
int count = freqMap.get(removed);
if (count == 1) {
freqMap.remove(removed);
} else {
freqMap.put(removed, count - 1);
}
}
}
System.out.println(maxUnique);
}
}