2779. Number of Adjacent Elements With the Same Color
My accepted Python solution to LeetCode problem 2779, Number of Adjacent Elements With the Same Color, running in 67ms.
- Difficulty: Medium
- Python
- Runtime 67ms
- Memory 46.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 67ms, memory 46.5MB, accepted 2025-12-31.
class Solution:
def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
colors = [0] * n
count = 0
result = []
for index, color in queries:
old_color = colors[index]
# Remove old contribution
if old_color != 0:
if index > 0 and colors[index - 1] == old_color:
count -= 1
if index < n - 1 and colors[index + 1] == old_color:
count -= 1
# Update color
colors[index] = color
# Add new contribution
if index > 0 and colors[index - 1] == color:
count += 1
if index < n - 1 and colors[index + 1] == color:
count += 1
result.append(count)
return result