2025-01-09 20:29:41 +08:00
< p > You are given an integer < code > n< / code > representing an array < code > colors< / code > of length < code > n< / code > where all elements are set to 0' s meaning < strong > uncolored< / strong > . You are also given a 2D integer array < code > queries< / code > where < code > queries[i] = [index< sub > i< / sub > , color< sub > i< / sub > ]< / code > . For the < code > i< sup > th< / sup > < / code > < strong > query< / strong > :< / p >
2023-05-15 17:43:00 +08:00
2025-01-09 20:29:41 +08:00
< ul >
< li > Set < code > colors[index< sub > i< / sub > ]< / code > to < code > color< sub > i< / sub > < / code > .< / li >
< li > Count adjacent pairs in < code > colors< / code > set to the same color (regardless of < code > color< sub > i< / sub > < / code > ).< / li >
< / ul >
2023-05-15 17:43:00 +08:00
2025-01-09 20:29:41 +08:00
< p > Return an array < code > answer< / code > of the same length as < code > queries< / code > where < code > answer[i]< / code > is the answer to the < code > i< sup > th< / sup > < / code > query.< / p >
2023-05-15 17:43:00 +08:00
< p > < / p >
< p > < strong class = "example" > Example 1:< / strong > < / p >
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]< / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > [0,1,1,0,2]< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< ul >
< li > Initially array colors = [0,0,0,0], where 0 denotes uncolored elements of the array.< / li >
< li > After the 1< sup > st< / sup > query colors = [2,0,0,0]. The count of adjacent pairs with the same color is 0.< / li >
< li > After the 2< sup > nd< / sup > query colors = [2,2,0,0]. The count of adjacent pairs with the same color is 1.< / li >
< li > After the 3< sup > rd< / sup > query colors = [2,2,0,1]. The count of adjacent pairs with the same color is 1.< / li >
< li > After the 4< sup > th< / sup > query colors = [2,1,0,1]. The count of adjacent pairs with the same color is 0.< / li >
< li > After the 5< sup > th< / sup > query colors = [2,1,1,1]. The count of adjacent pairs with the same color is 2.< / li >
< / ul >
< / div >
2023-05-15 17:43:00 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > n = 1, queries = [[0,100000]]< / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > [0]< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< p > After the 1< sup > st< / sup > query colors = [100000]. The count of adjacent pairs with the same color is 0.< / p >
< / div >
2023-05-15 17:43:00 +08:00
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > 1 < = n < = 10< sup > 5< / sup > < / code > < / li >
< li > < code > 1 < = queries.length < = 10< sup > 5< / sup > < / code > < / li >
< li > < code > queries[i].length == 2< / code > < / li >
< li > < code > 0 < = index< sub > i< / sub > < = n - 1< / code > < / li >
< li > < code > 1 < = color< sub > i< / sub > < = 10< sup > 5< / sup > < / code > < / li >
< / ul >