Skip to content

Commit 3eb9a19

Browse files
committed
commit
1 parent 94b30f0 commit 3eb9a19

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int maxDistance(int[] colors) {
3+
// Our approach is comparing the length value from left and right
4+
int n = colors.length;
5+
6+
int left = colors[0];
7+
int right = colors[n - 1];
8+
9+
int from_left = 0, from_right = 0;
10+
11+
// Search length from left
12+
for (int curr = 0; curr < n; curr++)
13+
if (colors[curr] != left) from_left = curr;
14+
15+
// Search length from right
16+
for (int curr = n - 1; curr >= 0; curr--)
17+
if (colors[curr] != right) from_right = (n - 1) - curr;
18+
19+
return Math.max(from_left, from_right);
20+
}
21+
}

0 commit comments

Comments
 (0)