Trains the technique from
LeetCode 2672Number of Adjacent Elements With the Same ColorThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A display shelf carries n tiles in a row, numbered 0 through n - 1. Every tile starts out blank, meaning it carries no colour whatsoever.
You are given queries. Its i-th entry names a position index_i followed by a colour colour_i. Work through the queries in the order given: query i paints the tile at index_i in colour colour_i, wiping out whatever that tile carried before.
Straight after each query, count the neighbouring tile pairs that carry the same colour. Two tiles are neighbours when their positions are one apart. A pair only counts when both of its tiles have been painted, so a blank tile never takes part in a counted pair, not even alongside another blank tile.
Return answer, where answer[i] is that count taken right after query i is applied.
Example 1
After the first query only tile 0 is painted, so no pair can count. The second query paints tile 1 in the same colour, giving one pair. Tile 3 is then painted but tile 2 is still blank, so the count stays at 1. Repainting tile 1 in colour 7 breaks that pair, and painting tile 2 in colour 7 forms a new one.
Example 2
A shelf of one tile has no neighbouring pairs at all, so both counts are 0.
Example 3
Tiles 0 and 2 both carry colour 4, but they are two apart and so are not neighbours. Painting tile 1 in colour 9 matches neither of them, so every count is 0.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def color_the_array(n: int, queries: list[list[int]]) -> list[int]:public int[] colorTheArray(int n, int[][] queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.