Skip to content
LC-2078 Easy LeetCode

2078. Two Furthest Houses With Different Colors

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 66% Topics: Array, Greedy
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def maxDistance(self, colors):
        """
        :type colors: List[int]
        :rtype: int
        """
        result = 0
        for i, x in enumerate(colors):
            if x != colors[0]:
                result = max(result, i)
            if x != colors[-1]:
                result = max(result, len(colors)-1-i)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT