Mid/SeniorArray

Number of Adjacent Elements With the Same Color

You are given an integer n representing an array colors of length n where all elements are set to 0, meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori].

For the i^th query:

  • Set colors[indexi] to colori.
  • Count the number of adjacent pairs in colors which have the same color, regardless of colori.

Return an array answer of the same length as queries where answer[i] is the answer to the i^th query.

Example 1
Inputn = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
Output[0,1,1,0,2]
After each query, the counts of adjacent pairs with the same color are 0, 1, 1, 0, and 2 respectively.
Example 2
Inputn = 1, queries = [[0,100000]]
Output[0]
With only one element, there are no adjacent pairs, so the count is 0 after the query.

Constraints

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

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate