1
class Solution {
2
public:
3
int maxDistance(vector<int> &colors) {
4
int Max = INT_MIN;
5
int N = colors.size();
6

7
// find the first house from the end which does not match the color of house
8
// at front
9
int j = N;
10
while (--j >= 0 && colors[0] == colors[j]) {
11
} // worst-case O(n)
12
Max = abs(j - 0);
13

14
// find the first house from the front which does not match the color of
15
// house at back
16
j = -1;
17
while (++j < N && colors[N - 1] == colors[j]) {
18
} // worst-case O(n)
19
Max = max(Max, abs(j - (N - 1)));
20

21
return Max;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0