There are n
houses evenly lined up on the street, and each house is beautifully painted. You are given a 0-indexed integer array colors
of length n
, where colors[i]
represents the color of the ith
house.
Return the maximum distance between two houses with different colors.
The distance between the ith
and jth
houses is abs(i - j)
, where abs(x)
is the absolute value of x
.
Example 1:
Input: colors = [1,1,1,6,1,1,1] Output: 3 Explanation: In the above image, color 1 is blue, and color 6 is red. The furthest two houses with different colors are house 0 and house 3. House 0 has color 1, and house 3 has color 6. The distance between them is abs(0 - 3) = 3. Note that houses 3 and 6 can also produce the optimal answer.
Example 2:
Input: colors = [1,8,3,8,3] Output: 4 Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green. The furthest two houses with different colors are house 0 and house 4. House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 - 4) = 4.
Example 3:
Input: colors = [0,1] Output: 1 Explanation: The furthest two houses with different colors are house 0 and house 1. House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 - 1) = 1.
Constraints:
n == colors.length
2 <= n <= 100
0 <= colors[i] <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy to find the two furthest houses with different colors is all about trying every single combination. We systematically compare each house with every other house to check their colors.
Here's how the algorithm would work step-by-step:
def furthest_houses_with_different_colors_brute_force(colors):
max_distance = 0
# Iterate through each house to compare it with every other
for first_house_index in range(len(colors)):
for second_house_index in range(len(colors)):
# Check if colors of houses are different
if colors[first_house_index] != colors[second_house_index]:
# Update max_distance if current distance is larger
current_distance = abs(first_house_index - second_house_index)
max_distance = max(max_distance, current_distance)
return max_distance
The core idea is to avoid checking every single pair of houses. Instead, we cleverly check only a few specific house pairings to find the largest possible distance between differently colored houses. We check the first house against the last and the last house against the first.
Here's how the algorithm would work step-by-step:
def furthestHousesWithDifferentColors(colors):
left_index = 0
right_index = len(colors) - 1
# If the first and last house colors differ, we have the answer.
if colors[left_index] != colors[right_index]:
return right_index - left_index
max_distance = 0
# Find the furthest house from the start with a different color
for index in range(right_index - 1, 0, -1):
if colors[left_index] != colors[index]:
max_distance = max(max_distance, index - left_index)
break
# Find the furthest house from the end with a different color
for index in range(1, right_index):
if colors[right_index] != colors[index]:
max_distance = max(max_distance, right_index - index)
break
return max_distance
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately, as no houses exist to compare. |
Array with only one house | Return 0, since no other house exists to have a different color. |
Array with only two houses of different colors, located at the extremes. | The algorithm should correctly identify these houses and return the distance which is colors.length - 1. |
All houses have the same color | Return 0, as there are no two houses with different colors. |
Houses are arranged such that different colors are clustered together with the same color filling the end of array | The algorithm should correctly traverse the array from both ends to find the furthest apart differently colored houses. |
Integer overflow if array size is close to Integer.MAX_VALUE | The problem states max size is 100, so integer overflow isn't a concern with the length of the array, but consider checking distance calculation to ensure no int overflow |
Large input size | The solution should have a time complexity of O(n) to ensure it scales efficiently with larger input sizes. |
Edge case with extreme boundary values of color values | Since the array only contains colors, the boundary values don't have any special effect other than the equality comparison. No special handling required. |