Skip to content

Commit 24b8b79

Browse files
Sync LeetCode submission Runtime - 112 ms (51.35%), Memory - 157.6 MB (45.95%)
1 parent 6445fad commit 24b8b79

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<p>You are given an integer <code>limit</code> and a 2D array <code>queries</code> of size <code>n x 2</code>.</p>
2+
3+
<p>There are <code>limit + 1</code> balls with <strong>distinct</strong> labels in the range <code>[0, limit]</code>. Initially, all balls are uncolored. For every query in <code>queries</code> that is of the form <code>[x, y]</code>, you mark ball <code>x</code> with the color <code>y</code>. After each query, you need to find the number of colors among the balls.</p>
4+
5+
<p>Return an array <code>result</code> of length <code>n</code>, where <code>result[i]</code> denotes the number of colors <em>after</em> <code>i<sup>th</sup></code> query.</p>
6+
7+
<p><strong>Note</strong> that when answering a query, lack of a color <em>will not</em> be considered as a color.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">[1,2,2,3]</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif" style="width: 455px; height: 145px;" /></p>
20+
21+
<ul>
22+
<li>After query 0, ball 1 has color 4.</li>
23+
<li>After query 1, ball 1 has color 4, and ball 2 has color 5.</li>
24+
<li>After query 2, ball 1 has color 3, and ball 2 has color 5.</li>
25+
<li>After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.</li>
26+
</ul>
27+
</div>
28+
29+
<p><strong class="example">Example 2:</strong></p>
30+
31+
<div class="example-block">
32+
<p><strong>Input:</strong> <span class="example-io">limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]</span></p>
33+
34+
<p><strong>Output:</strong> <span class="example-io">[1,2,2,3,4]</span></p>
35+
36+
<p><strong>Explanation:</strong></p>
37+
38+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif" style="width: 457px; height: 144px;" /></strong></p>
39+
40+
<ul>
41+
<li>After query 0, ball 0 has color 1.</li>
42+
<li>After query 1, ball 0 has color 1, and ball 1 has color 2.</li>
43+
<li>After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.</li>
44+
<li>After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.</li>
45+
<li>After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5.</li>
46+
</ul>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>1 &lt;= limit &lt;= 10<sup>9</sup></code></li>
54+
<li><code>1 &lt;= n == queries.length &lt;= 10<sup>5</sup></code></li>
55+
<li><code>queries[i].length == 2</code></li>
56+
<li><code>0 &lt;= queries[i][0] &lt;= limit</code></li>
57+
<li><code>1 &lt;= queries[i][1] &lt;= 10<sup>9</sup></code></li>
58+
</ul>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<int> queryResults(int limit, vector<vector<int>>& queries) {
4+
unordered_map<int, int> color, freq;
5+
int colors = 0;
6+
vector<int> res;
7+
for (vector<int>& query : queries) {
8+
int x = query[0];
9+
int y = query[1];
10+
11+
int prevCol = color[x];
12+
if (prevCol == 0) {
13+
color[x] = y;
14+
freq[y]++;
15+
if (freq[y] == 1) colors++;
16+
}
17+
else if (prevCol != y) {
18+
color[x] = y;
19+
freq[prevCol]--;
20+
freq[y]++;
21+
if (freq[prevCol] == 0) colors--;
22+
if (freq[y] == 1) colors++;
23+
}
24+
25+
res.push_back(colors);
26+
}
27+
28+
return res;
29+
}
30+
};

0 commit comments

Comments
 (0)