-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjava-dequeue.java
59 lines (56 loc) · 1.79 KB
/
java-dequeue.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
49
50
51
52
53
54
55
56
57
58
59
// Java Deque
// 🟠 Medium
//
// https://www.hackerrank.com/challenges/java-dequeue
//
// Tags: Data Structures
import java.util.*;
public class Solution {
/*
* Use a queue to push in one end and pop from the other, when we push a
* value we also add 1 to its count in the hashmap, when the queue has
* more than m values, we pop a value, and remove one from its count
* from the hash map, if the count gets to zero, we remove the entry.
* After pushing and popping, we get the count of unique values in the
* current subarray and check that against the current maximum.
*
* Time complexity: O(n) - We iterate over the n values, we push and
* pop them from the deque and push/pop them from the hashmap, all
* of them O(1) operations.
* Space complexity: O(n) - Both the queue and the hashmap will grow
* to the same size as the input.
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Deque<Integer> deque = new ArrayDeque<Integer>();
int n = in.nextInt();
int m = in.nextInt();
// Use a hashmap to count the number of unique values in the subarray.
HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
int res = 0;
for (int i = 0; i < n; i++) {
int num = in.nextInt();
deque.add(num);
if (count.containsKey(num)) {
count.put(num, count.get(num) + 1);
} else {
count.put(num, 1);
}
if (deque.size() > m) {
int popped = deque.pop();
count.put(popped, count.get(popped) - 1);
if (count.get(popped) == 0) {
count.remove(popped);
}
}
if (count.size() > res) {
res = count.size();
if (res == m) {
break;
}
}
}
System.out.println(res);
in.close();
}
}