Number of adjacent elements with the same color

May 18, 2023

array-and-hashmap

Problem URL: Number of adjacent elements with the same color

We can use a hashmap to store the indices of each color. Then, we can iterate through the array and check if the adjacent elements have the same color.

class Solution:
    def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
        res = []
        lookup, count = collections.defaultdict(int), 0
        for i, color in queries:
            if lookup[i]: 
                count -= (lookup[i-1] == lookup[i]) + (lookup[i+1] == lookup[i])

            lookup[i] = color
            if lookup[i]:
                count += (lookup[i-1] == lookup[i]) + (lookup[i+1] == lookup[i])

            res.append(count)

        return res        

Time complexity: O(n) where n is the length of the queries array.
Space complexity: O(n) where n is the length of the queries array.