-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColors.java
More file actions
46 lines (45 loc) · 1.54 KB
/
Colors.java
File metadata and controls
46 lines (45 loc) · 1.54 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
44
45
46
import java.util.*;
public class Colors {
public static int[] distinct(int limit , int[][] queries){
int n = queries.length;
HashMap<Integer,Integer> ballColor = new HashMap<>();
HashMap<Integer,Integer> colorFreq = new HashMap<>();
int[] ans = new int[n];
for (int i = 0; i < queries.length; i++) {
int ball = queries[i][0];
int color = queries[i][1];
if (ballColor.containsKey(ball)) {
int prevColor = ballColor.get(ball);
if (prevColor == color) {
ans[i] = colorFreq.size();
}
if (colorFreq.get(prevColor)==1) {
colorFreq.remove(prevColor);
}
else{
colorFreq.put(prevColor, colorFreq.get(prevColor)-1);
}
}
ballColor.put(ball, color);
colorFreq.put(color, colorFreq.getOrDefault(color, 0)+1);
ans[i] = colorFreq.size();
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int limit = sc.nextInt();
int n = sc.nextInt();
int[][] queries = new int[n][2];
for (int i = 0; i < queries.length; i++) {
for (int j = 0; j < 2; j++) {
queries[i][j] = sc.nextInt();
}
}
int[] result = distinct(limit,queries);
for (int i : result) {
System.out.print(i+" ");
}
sc.close();
}
}