[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

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a Design System implementation for Android — Part 2

Common bottom navigation bar — Flutter

AWS EC2: Elastic Compute Cloud 3rd Part

How I learnt to love and hate Terraform in the past few weeks

Libraries In Programing Languages

Git이 SVN보다 나은 이유.

Special Formatting For Strings In Python

Tableau OEM and Embedded Community

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.

More from Medium

Binary Search

Basic Data Structure and Algorithms in Python

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Regular Expression Matching…

Heaps, the python way

heap