2078. Two Furthest Houses With Different Colors LeetCode Solution
In this guide, you will get 2078. Two Furthest Houses With Different Colors LeetCode Solution with the best time and space complexity. The solution to Two Furthest Houses With Different Colors problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Two Furthest Houses With Different Colors solution in C++
- Two Furthest Houses With Different Colors solution in Java
- Two Furthest Houses With Different Colors solution in Python
- Additional Resources
Problem Statement of Two Furthest Houses With Different Colors
There are n houses evenly lined up on the street, and each house is beautifully painted. You are given a 0-indexed integer array colors of length n, where colors[i] represents the color of the ith house.
Return the maximum distance between two houses with different colors.
The distance between the ith and jth houses is abs(i – j), where abs(x) is the absolute value of x.
Example 1:
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.
Example 2:
Input: colors = [1,8,3,8,3]
Output: 4
Explanation: In the above image, color 1 is blue, color 8 is yellow, and color 3 is green.
The furthest two houses with different colors are house 0 and house 4.
House 0 has color 1, and house 4 has color 3. The distance between them is abs(0 – 4) = 4.
Example 3:
Input: colors = [0,1]
Output: 1
Explanation: The furthest two houses with different colors are house 0 and house 1.
House 0 has color 0, and house 1 has color 1. The distance between them is abs(0 – 1) = 1.
Constraints:
n == colors.length
2 <= n <= 100
0 <= colors[i] <= 100
Test data are generated such that at least two houses have different colors.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2078. Two Furthest Houses With Different Colors LeetCode Solution in C++
class Solution {
public:
int maxDistance(vector<int>& colors) {
// The maximum distance always includes either the first or the last house.
const int n = colors.size();
int i = 0; // the leftmost index, where colors[i] != colors[-1]
int j = n - 1; // the rightmost index, where colors[j] != colors[0]
while (colors[i] == colors.back())
++i;
while (colors[j] == colors.front())
--j;
return max(n - 1 - i, j);
}
};
/* code provided by PROGIEZ */
2078. Two Furthest Houses With Different Colors LeetCode Solution in Java
class Solution {
public int maxDistance(int[] colors) {
final int n = colors.length;
int i = 0; // the leftmost index, where colors[i] != colors[n - 1]
int j = n - 1; // the rightmost index, where colors[j] != colors[0]
while (colors[i] == colors[n - 1])
++i;
while (colors[j] == colors[0])
--j;
return Math.max(n - 1 - i, j);
}
}
// code provided by PROGIEZ
2078. Two Furthest Houses With Different Colors LeetCode Solution in Python
class Solution:
def maxDistance(self, colors: list[int]) -> int:
# The maximum distance always includes either the first or the last house.
n = len(colors)
i = 0 # the leftmost index, where colors[i] != colors[-1]
j = n - 1 # the rightmost index, where colors[j] != colors[0]
while colors[i] == colors[-1]:
i += 1
while colors[j] == colors[0]:
j -= 1
return max(n - 1 - i, j)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.