1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-20 20:46:47 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/有相同颜色的相邻元素数目 [number-of-adjacent-elements-with-the-same-color].html
2025-09-29 14:43:44 +08:00

54 lines
2.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给定一个整数&nbsp;<code>n</code>&nbsp;表示一个长度为&nbsp;<code>n</code> 的数组&nbsp; <code>colors</code>,初始所有元素均为 0 ,表示是 <strong>未染色 </strong>的。同时给定一个二维整数数组&nbsp;<code>queries</code>,其中&nbsp;<code>queries[i] = [index<sub>i</sub>, color<sub>i</sub>]</code>。对于第&nbsp;<code>i</code>&nbsp;<strong>查询</strong></p>
<ul>
<li>&nbsp;<code>colors[index<sub>i</sub>]</code>&nbsp;染色为 <code>color<sub>i</sub></code></li>
<li>统计&nbsp;<code>colors</code>&nbsp;中颜色相同的相邻对的数量(无论 <code>color<sub>i</sub></code>)。</li>
</ul>
<p>请你返回一个长度与 <code>queries</code>&nbsp;相等的数组<em>&nbsp;</em><code>answer</code><em>&nbsp;</em>,其中<em>&nbsp;</em><code>answer[i]</code>是前 <code>i</code>&nbsp;个操作的答案。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]</span></p>
<p><span class="example-io"><b>输出:</b>[0,1,1,0,2]</span></p>
<p><strong>解释:</strong></p>
<ul>
<li>一开始 colors = [0,0,0,0],其中 0 表示数组中未染色的元素。</li>
<li>在第 1 次查询后&nbsp;colors = [2,0,0,0]。颜色相同的相邻对的数量是 0。</li>
<li>在第 2&nbsp;次查询后 colors = [2,2,0,0]。颜色相同的相邻对的数量是 1。</li>
<li>在第 3&nbsp;次查询后 colors = [2,2,0,1]。颜色相同的相邻对的数量是 1。</li>
<li>在第 4&nbsp;次查询后 colors = [2,1,0,1]。颜色相同的相邻对的数量是 0。</li>
<li>在第 5&nbsp;次查询后 colors = [2,1,1,1]。颜色相同的相邻对的数量是 2。</li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 1, queries = [[0,100000]]</span></p>
<p><span class="example-io"><b>输出:</b>[0]</span></p>
<p><strong>解释:</strong></p>
<p>在第一次查询后&nbsp;colors = [100000]。颜色相同的相邻对的数量是 0。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>
<li><code>queries[i].length&nbsp;== 2</code></li>
<li><code>0 &lt;= index<sub>i</sub>&nbsp;&lt;= n - 1</code></li>
<li><code>1 &lt;=&nbsp; color<sub>i</sub>&nbsp;&lt;= 10<sup>5</sup></code></li>
</ul>