Class Solution
- java.lang.Object
-
- g2601_2700.s2672_number_of_adjacent_elements_with_the_same_color.Solution
-
public class Solution extends Object
2672 - Number of Adjacent Elements With the Same Color.Medium
There is a 0-indexed array
numsof lengthn. Initially, all elements are uncolored (has a value of0).You are given a 2D integer array
querieswherequeries[i] = [indexi, colori].For each query, you color the index
indexiwith the colorcoloriin the arraynums.Return an array
answerof the same length asquerieswhereanswer[i]is the number of adjacent elements with the same color after theithquery.More formally,
answer[i]is the number of indicesj, such that0 <= j < n - 1andnums[j] == nums[j + 1]andnums[j] != 0after theithquery.Example 1:
Input: n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
Output: [0,1,1,0,2]
Explanation: Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
- After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
- After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
- After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
- After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.
Example 2:
Input: n = 1, queries = [[0,100000]]
Output: [0]
Explanation: Initially array nums = [0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.
Constraints:
1 <= n <= 1051 <= queries.length <= 105queries[i].length == 20 <= indexi <= n - 11 <= colori <= 105
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]colorTheArray(int n, int[][] q)
-