Number of Adjacent Elements With the Same Color - Complete Solution Guide
Number of Adjacent Elements With the Same Color is LeetCode problem 2672, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored . You are also given a 2D integer array queries where queries[i] = [index i , color i ] . For the i th query : Set colors[index i ] to color i . Count the number of adjacent pairs in colors which have the same color (regardless of color i ). Return an array answer of the same length as queries where answer[i] is the answer to the i th query. Example 1: Input: n = 4, queries = [
Detailed Explanation
The problem presents an array `colors` of length `n`, initialized with all elements set to 0, representing an uncolored state. A series of `queries` is given, where each query consists of an `index` and a `color`. For each query, the element at the specified `index` in the `colors` array is updated with the given `color`. After each update, the goal is to count the number of adjacent pairs of elements in the `colors` array that have the same color. The final output is an array `answer` where `answer[i]` represents the count of adjacent pairs with the same color after processing the `i`-th query.
Solution Approach
The solution maintains an array `colors` representing the colors at each index and a variable `current_count` to store the number of adjacent pairs with the same color. For each query, before updating the color at the given `index`, we check if the current color at that `index` is non-zero. If it is, we check if it forms an adjacent pair on the left and right and decrement the `current_count` accordingly. Then, the color at the given `index` is updated to the new color. Finally, we check if the new color at the given `index` forms an adjacent pair on the left and right and increment the `current_count` accordingly. The updated `current_count` is added to the answer array.
Step-by-Step Algorithm
- Step 1: Initialize an array `colors` of size `n` with all elements set to 0.
- Step 2: Initialize `current_count` to 0 to keep track of the number of adjacent pairs with the same color.
- Step 3: Iterate through each query in the `queries` array.
- Step 4: For each query, extract the `index` and `new_color`.
- Step 5: Store the old color at the current `index` to the `old_color` variable.
- Step 6: If the `old_color` and `new_color` are the same, append the `current_count` to the `answer` array and continue to the next query.
- Step 7: If `old_color` is not 0, check if there is an adjacent element on the left (if index > 0) with the same `old_color`, if so decrement `current_count`.
- Step 8: If `old_color` is not 0, check if there is an adjacent element on the right (if index < n-1) with the same `old_color`, if so decrement `current_count`.
- Step 9: Update the color at the given `index` in the `colors` array to the `new_color`.
- Step 10: Check if there is an adjacent element on the left (if index > 0) with the same `new_color`, if so increment `current_count`.
- Step 11: Check if there is an adjacent element on the right (if index < n-1) with the same `new_color`, if so increment `current_count`.
- Step 12: Append the `current_count` to the `answer` array.
- Step 13: Return the `answer` array.
Key Insights
- Insight 1: The problem requires maintaining a running count of adjacent elements with the same color as the array is updated.
- Insight 2: The color update at an index can only affect the adjacent pairs involving that index (index-1, index) and (index, index+1).
- Insight 3: Recomputing the entire adjacent pair count after each query would be inefficient. The running count must be updated incrementally.
Complexity Analysis
Time Complexity: O(q)
Space Complexity: O(n)
Topics
This problem involves: Array.
Companies
Asked at: Capital One, Roblox, Visa.