[LeetCode]#2078. Two Furthest Houses With Different Colors

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.
  1. Use enumerate find location and number in list.
  2. Check location 0 number is equal or not.
  3. If yes, distance is 0.
  4. If yes location index -0.
  5. output max one.
class Solution:
def maxDistance(self, colors):
d=0
for i,j in enumerate(colors):
for a,b in enumerate(colors):
if j!=b:
if abs(i-a)>d:
d=abs(i-a)
return d

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.