classSolution { public: intmaxDistance(vector<int>& colors){ int res = 0 , idx = -1; int n = colors.size(); for(int i = 0 ;i<n-1 ;++i){ for(int j = i+1;j<n;++j){ if(colors[i]!=colors[j]) res = max(res, j-i); } } return res; } };
1 2 3 4 5 6 7 8 9
classSolution: defmaxDistance(self, colors: List[int]) -> int: ret , n= 0 , len(colors) for i inrange(n-1): for j inrange(i+1, n): if colors[i]!=colors[j]: ret = max(ret, j-i) return ret