Environment: Python 3.8
Key technique: enumerate, if
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
colors[i] represents the color of the
Return the maximum distance between two houses with different colors.
The distance between the
jth houses is
abs(i - j), where
abs(x) is the absolute value of
Input: colors = [1,1,1,6,1,1,1]
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.
- Use enumerate find location and number in list.
- Check location 0 number is equal or not.
- If yes, distance is 0.
- If yes location index -0.
- output max one.
def maxDistance(self, colors):
for i,j in enumerate(colors):
for a,b in enumerate(colors):