Skip to content

Commit f52fdb0

Browse files
committed
Add solution #3160
1 parent bf2d661 commit f52fdb0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567
3105|[Longest Strictly Increasing or Strictly Decreasing Subarray](./3105-longest-strictly-increasing-or-strictly-decreasing-subarray.js)|Easy|
568568
3110|[Score of a String](./3110-score-of-a-string.js)|Easy|
569569
3151|[Special Array I](./3151-special-array-i.js)|Easy|
570+
3160|[Find the Number of Distinct Colors Among the Balls](./3160-find-the-number-of-distinct-colors-among-the-balls.js)|Medium|
570571
3223|[Minimum Length of String After Operations](./3223-minimum-length-of-string-after-operations.js)|Medium|
571572
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
572573
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 3160. Find the Number of Distinct Colors Among the Balls
3+
* https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer limit and a 2D array queries of size n x 2.
7+
*
8+
* There are limit + 1 balls with distinct labels in the range [0, limit]. Initially,
9+
* all balls are uncolored. For every query in queries that is of the form [x, y],
10+
* you mark ball x with the color y. After each query, you need to find the number
11+
* of distinct colors among the balls.
12+
*
13+
* Return an array result of length n, where result[i] denotes the number of distinct
14+
* colors after ith query.
15+
*
16+
* Note that when answering a query, lack of a color will not be considered as a color.
17+
*/
18+
19+
/**
20+
* @param {number} limit
21+
* @param {number[][]} queries
22+
* @return {number[]}
23+
*/
24+
var queryResults = function(limit, queries) {
25+
const result = [];
26+
27+
for (let i = 0, colors = new Map(), counts = new Map(); i < queries.length; i++) {
28+
const [index, color] = queries[i];
29+
if (colors.has(index)) {
30+
const prev = colors.get(index);
31+
counts.set(prev, counts.get(prev) - 1);
32+
if (!counts.get(prev)) counts.delete(prev);
33+
}
34+
colors.set(index, color);
35+
counts.set(color, (counts.get(color) ?? 0) + 1);
36+
result.push(counts.size);
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
 (0)