LeetCode 2672: Number of Adjacent Elements With the Same Color Solution

Master LeetCode problem 2672 (Number of Adjacent Elements With the Same Color), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2672. Number of Adjacent Elements With the Same Color

Medium

Problem Explanation

Explanation:

To solve this problem, we need to iterate through the queries, update the colors array according to each query, and then count the adjacent elements with the same color.

We can achieve this by maintaining a count of adjacent elements with the same color while updating the colors array. For each query, we update the colors array and check if the adjacent elements have the same color. If they do, we increment the count.

Algorithm:

  1. Initialize an array colors of length n filled with zeros.
  2. Initialize a variable count to store the count of adjacent elements with the same color.
  3. Iterate through each query:
    • Update the colors array with the specified color at the given index.
    • Update the count by checking the adjacent elements in the colors array.
    • Store the current count in the result array.
  4. Return the result array.

Time Complexity: O(N) where N is the number of queries. Space Complexity: O(N) for the colors array and result array.

Solution Code

class Solution {
    public int[] numColor(int n, int[][] queries) {
        int[] colors = new int[n];
        int[] result = new int[queries.length];
        int count = 0;
        
        for (int i = 0; i < queries.length; i++) {
            int index = queries[i][0];
            int color = queries[i][1];
            
            colors[index] = color;
            
            if (index > 0 && colors[index - 1] == color) {
                count++;
            }
            if (index < n - 1 && colors[index + 1] == color) {
                count++;
            }
            
            result[i] = count;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2672 (Number of Adjacent Elements With the Same Color)?

This page provides optimized solutions for LeetCode problem 2672 (Number of Adjacent Elements With the Same Color) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2672 (Number of Adjacent Elements With the Same Color)?

The time complexity for LeetCode 2672 (Number of Adjacent Elements With the Same Color) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2672 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2672 in Java, C++, or Python.

Back to LeetCode Solutions