-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUniqueNumsInSubarray.java
More file actions
32 lines (23 loc) · 871 Bytes
/
Copy pathUniqueNumsInSubarray.java
File metadata and controls
32 lines (23 loc) · 871 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
import java.util.Deque;
import java.util.LinkedList;
// https://www.hackerrank.com/challenges/java-dequeue/problem
public class UniqueNumsInSubarray {
private int countUniqueNumsInSubarrays(int[] nums, int k) {
Deque<Integer> deque = new LinkedList<>();
int maxUniqueCount = 0;
for (int i = 0; i < nums.length; i++) {
// if deque's left entry is outside the window then pop it out
while (!deque.isEmpty() && i - deque.peekFirst() >= k) {
deque.removeFirst();
}
while (!deque.isEmpty() && nums[deque.peekLast()] == nums[i]) {
deque.removeLast();
}
deque.addLast(i);
if (i >= k - 1) {
maxUniqueCount = Math.max(maxUniqueCount, deque.size());
}
}
return maxUniqueCount;
}
}