Skip to content

Commit 8b40239

Browse files
committed
Sync LeetCode submission Runtime - 67 ms (78.22%), Memory - 63.1 MB (80.80%)
1 parent 28d779a commit 8b40239

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
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 <strong>distinct</strong> 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 distinct 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+
# Approach 2: Two Hash Maps
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
6+
class Solution:
7+
def queryResults(self, limit: int, queries: List[List[int]]) -> List[int]:
8+
n = len(queries)
9+
result = []
10+
color_map = {}
11+
ball_map = {}
12+
13+
for i in range(n):
14+
ball, color = queries[i]
15+
16+
# If the ball is already colored
17+
if ball in ball_map:
18+
prev_color = ball_map[ball]
19+
color_map[prev_color] -= 1
20+
21+
if color_map[prev_color] == 0:
22+
del color_map[prev_color]
23+
24+
ball_map[ball] = color
25+
color_map[color] = color_map.get(color, 0) + 1
26+
27+
result.append(len(color_map))
28+
29+
return result
30+

0 commit comments

Comments
 (0)