1class Solution {2public:3int maxDistance(vector<int> &colors) {4int Max = INT_MIN;5int N = colors.size();67// find the first house from the end which does not match the color of house8// at front9int j = N;10while (--j >= 0 && colors[0] == colors[j]) {11} // worst-case O(n)12Max = abs(j - 0);1314// find the first house from the front which does not match the color of15// house at back16j = -1;17while (++j < N && colors[N - 1] == colors[j]) {18} // worst-case O(n)19Max = max(Max, abs(j - (N - 1)));2021return Max;22}23};