-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdNums.java
59 lines (46 loc) · 1.57 KB
/
dNums.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
/*
You are given an array of N integers, A1, A2 ,…, AN and an integer K. Return the of count of distinct numbers in all windows of size K.
Formally, return an array of size N-K+1 where i’th element in this array contains number of distinct elements in sequence Ai, Ai+1 ,…, Ai+k-1.
Note:
If K > N, return empty array.
For example,
A=[1, 2, 1, 3, 4, 3] and K = 3
All windows of size K are
[1, 2, 1]
[2, 1, 3]
[1, 3, 4]
[3, 4, 3]
So, we return an array [2, 3, 3, 2].
*/
public static ArrayList<Integer> dNums(ArrayList<Integer> a, int b) {
if(a==null || a.isEmpty() || b==0)
return null;
ArrayList<Integer> result = new ArrayList<Integer>();
if(a.size()< b)
return result;
HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
int i=0;
for( i= 0; i<b; i++)
{
if(!hashMap.containsKey(a.get(i)))
hashMap.put(a.get(i), 1);
else
hashMap.put(a.get(i), hashMap.get(a.get(i))+1);
}
result.add(hashMap.size());
if(i==a.size())
return result;
for( int j=i; j<a.size(); j++)
{
if(hashMap.get(a.get(j-b))==1)
hashMap.remove(a.get(j-b));
else
hashMap.put(a.get(j-b), hashMap.get(a.get(j-b))-1);
if(!hashMap.containsKey(a.get(j)))
hashMap.put(a.get(j), 1);
else
hashMap.put(a.get(j), hashMap.get(a.get(j))+1);
result.add(hashMap.size());
}
return result;
}