1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/所有球里面不同颜色的数目(English) [find-the-number-of-distinct-colors-among-the-balls].html
2024-06-05 08:50:06 +08:00

59 lines
2.7 KiB
HTML

<p>You are given an integer <code>limit</code> and a 2D array <code>queries</code> of size <code>n x 2</code>.</p>
<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>
<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>
<p><strong>Note</strong> that when answering a query, lack of a color <em>will not</em> be considered as a color.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">limit = 4, queries = [[1,4],[2,5],[1,3],[3,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif" style="width: 455px; height: 145px;" /></p>
<ul>
<li>After query 0, ball 1 has color 4.</li>
<li>After query 1, ball 1 has color 4, and ball 2 has color 5.</li>
<li>After query 2, ball 1 has color 3, and ball 2 has color 5.</li>
<li>After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">limit = 4, queries = [[0,1],[1,2],[2,2],[3,4],[4,5]]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,2,3,4]</span></p>
<p><strong>Explanation:</strong></p>
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif" style="width: 457px; height: 144px;" /></strong></p>
<ul>
<li>After query 0, ball 0 has color 1.</li>
<li>After query 1, ball 0 has color 1, and ball 1 has color 2.</li>
<li>After query 2, ball 0 has color 1, and balls 1 and 2 have color 2.</li>
<li>After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4.</li>
<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>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= limit &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= n == queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length == 2</code></li>
<li><code>0 &lt;= queries[i][0] &lt;= limit</code></li>
<li><code>1 &lt;= queries[i][1] &lt;= 10<sup>9</sup></code></li>
</ul>