Skip to content

Practice Number of Adjacent Elements With the Same Color

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2672
LeetCode Problem

Number of Adjacent Elements With the Same Color

Problem Statement

Start with n uncolored positions, represented by 0. Process each [index,color] query by assigning that color at the index, replacing its previous color. After each assignment, count adjacent pairs that share the same nonzero color across the entire array. Return those counts in query order.

Source: Number of Adjacent Elements With the Same Color on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
Output
[0,1,1,0,2]

# Example 2

Input
n = 1, queries = [[0,100000]]
Output
[0]

Constraints

1 <= n <= 10^5
1 <= queries.length <= 10^5
queries[i].length == 2
0 <= indexi <= n - 1
1 <=  colori <= 10^5

Related LeetCode Problems

Practice more problems similar to Number of Adjacent Elements With the Same Color.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.