2199. Two Furthest Houses With Different Colors
My accepted Python solution to LeetCode problem 2199, Two Furthest Houses With Different Colors, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17MB, accepted 2025-12-29.
class Solution:
def maxDistance(self, colors: List[int]) -> int:
# Max distance must involve either first or last house
# Time: O(n), Space: O(1)
n = len(colors)
# Find furthest from first house with different color
max_dist = 0
for i in range(n - 1, -1, -1):
if colors[i] != colors[0]:
max_dist = max(max_dist, i)
break
# Find furthest from last house with different color
for i in range(n):
if colors[i] != colors[n - 1]:
max_dist = max(max_dist, n - 1 - i)
break
return max_dist