Skip to content

Commit 70d1c41

Browse files
authored
Create Find the Number of Distinct Colors Among the Balls.java
1 parent d9a05d9 commit 70d1c41

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int[] queryResults(int limit, int[][] queries) {
3+
Map<Integer, Integer> ballToColor = new HashMap<>();
4+
Map<Integer, Integer> colorToBallCount = new HashMap<>();
5+
int n = queries.length;
6+
int[] result = new int[n];
7+
for (int i = 0; i < queries.length; i++) {
8+
int ball = queries[i][0];
9+
int color = queries[i][1];
10+
if (ballToColor.containsKey(ball)) {
11+
int prevColor = ballToColor.get(ball);
12+
colorToBallCount.put(prevColor, colorToBallCount.get(prevColor) - 1);
13+
if (colorToBallCount.get(prevColor) == 0) {
14+
colorToBallCount.remove(prevColor);
15+
}
16+
}
17+
ballToColor.put(ball, color);
18+
colorToBallCount.put(color, colorToBallCount.getOrDefault(color, 0) + 1);
19+
result[i] = colorToBallCount.size();
20+
}
21+
return result;
22+
}
23+
}

0 commit comments

Comments
 (0)