leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit a833d36250592d632c5bc68553632df5acdedfaf
parent e7fdd7d96208bf27d83bbedf8d8d3fba13274e2f
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun, 19 Apr 2026 20:18:50 -0700

2078. two furthest houses with different colors

Diffstat:
Atwo_furthest_houses_diff_colour.c | 34++++++++++++++++++++++++++++++++++
Atwo_furthest_houses_diff_colour.py | 27+++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/two_furthest_houses_diff_colour.c b/two_furthest_houses_diff_colour.c @@ -0,0 +1,34 @@ +// 2078. Two Furthest Houses With Different Colors +#include "leetcode.h" + +/* + * 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 i'th house. return + * the maximum distance between two houses with different colors. the distance + * between the i'th and the j'th house is 'abs(i - j)'. + */ + +int maxDistance(int *colors, int colorsSize) { + int left = 0, right = colorsSize - 1; + while (colors[left] == colors[colorsSize - 1]) + left++; + while (colors[right] == colors[0]) + right--; + return fmax(right, colorsSize - 1 - left); +} + +int main() { + int c1[] = {1, 1, 1, 6, 1, 1, 1}; + int c2[] = {1, 8, 3, 8, 3}; + int c3[] = {0, 1}; + int r1 = maxDistance(c1, ARRAY_SIZE(c1)); + int r2 = maxDistance(c2, ARRAY_SIZE(c2)); + int r3 = maxDistance(c3, ARRAY_SIZE(c3)); + printf("%d\n", r1); + assert(r1 == 3); + printf("%d\n", r2); + assert(r2 == 4); + printf("%d\n", r3); + assert(r3 == 1); +} diff --git a/two_furthest_houses_diff_colour.py b/two_furthest_houses_diff_colour.py @@ -0,0 +1,27 @@ +# 2078. 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 i'th house. return +the maximum distance between two houses with different colors. the distance +between the i'th and the j'th house is 'abs(i - j)'. +""" + + +class Solution: + def maxDistance(self, colors: List[int]) -> int: + ans = 0 + for x, y in enumerate(colors): + if y != colors[0]: + ans = max(ans, x) + if y != colors[-1]: + ans = max(ans, len(colors) - 1 - x) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxDistance(colors=[1, 1, 1, 6, 1, 1, 1])) + print(obj.maxDistance(colors=[1, 8, 3, 8, 3])) + print(obj.maxDistance(colors=[0, 1]))