Two Furthest Houses With Different Colors (LeetCode 2078): Solution and Explanation
Houses stand in a row, each with a color. Return the largest index distance between two houses whose colors differ.
Intuition
Trying every pair is O(n^2). That passes for n = 100, but there is a cleaner argument that needs only two short scans.
Take any pair i < j with different colors that uses neither end. House 0 differs from at least one of them, because they differ from each other. Pairing house 0 with j (if it differs from j) or swapping in the last house for j gives a pair at least as wide. So some best pair always contains house 0 or house n - 1.
With house 0 fixed, the best partner is the rightmost house whose color differs from house 0. With the last house fixed, the best partner is the leftmost house whose color differs from it.
Walk a pointer in from each end until the color changes, and take the larger of the two distances.
Approach: Anchor each end and scan inward
- Set
right = n - 1and move it left whilecolors[right] == colors[0]. The pair (0, right) has distanceright. - Set
left = 0and move it right whilecolors[left] == colors[n - 1]. The pair (left, n - 1) has distancen - 1 - left. - Return
max(right, n - 1 - left).
Step-by-step example
colors = [1,1,1,6,1,1,1]
- Anchor house 0 (color 1). right starts at 6: color 1 matches, move to 5, then 4, both color 1.
- right = 3 has color 6, which differs. Distance from house 0 is 3.
- Anchor house 6 (color 1). left starts at 0: color 1 matches, move to 1, then 2.
- left = 3 has color 6, which differs. Distance to house 6 is 6 - 3 = 3.
- max(3, 3) = 3.
Code: Python, Java, C++
1class Solution:
2 def maxDistance(self, colors: List[int]) -> int:
3 n = len(colors)
4 right = n - 1
5 while colors[right] == colors[0]:
6 right -= 1
7 left = 0
8 while colors[left] == colors[n - 1]:
9 left += 1
10 return max(right, n - 1 - left)1class Solution {
2 public int maxDistance(int[] colors) {
3 int n = colors.length;
4 int right = n - 1;
5 while (colors[right] == colors[0]) {
6 right--;
7 }
8 int left = 0;
9 while (colors[left] == colors[n - 1]) {
10 left++;
11 }
12 return Math.max(right, n - 1 - left);
13 }
14}1class Solution {
2public:
3 int maxDistance(vector<int>& colors) {
4 int n = colors.size();
5 int right = n - 1;
6 while (colors[right] == colors[0]) {
7 right--;
8 }
9 int left = 0;
10 while (colors[left] == colors[n - 1]) {
11 left++;
12 }
13 return max(right, n - 1 - left);
14 }
15};Time and space complexity
O(n)
Each pointer moves at most n - 1 steps.
O(1)
Only two indices are kept.
Other approaches
Check every pair (time O(n^2), space O(1))
Two nested loops over i < j, keeping the largest j - i with different colors. Fine for n = 100 but quadratic.
Single pass with both anchors (time O(n), space O(1))
Loop once over i and, for each house, update the answer with i if it differs from house 0 and with n - 1 - i if it differs from the last house.
Common mistakes
- Only checking pairs against house 0. When the differing houses all sit near the start, the pair with the last house is wider.
- Returning n - 1 without checking that the two end houses actually differ.
- Comparing neighbors instead of comparing against a fixed end. The question is about distance, not about where colors change.
Frequently asked questions
Why must the best pair touch an end of the street?
If i and j differ, house 0 differs from at least one of them, and the last house differs from at least one of them. Replacing the inner house with the matching end keeps the colors different and does not shrink the distance.
Can the scans run off the array?
No. The problem guarantees at least two colors, so some house differs from house 0 and some house differs from the last house, which stops each loop.
What if the first and last houses already differ?
Then right stays at n - 1 immediately and the answer is n - 1, the largest possible distance.