-
Notifications
You must be signed in to change notification settings - Fork 0
/
SolutionBefore.java
38 lines (36 loc) · 1.06 KB
/
SolutionBefore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package org.moro.pages;
/**
* Created by eyal on 14/11/2016.
*/
class SolutionBefore {
public static void main(String[] args) {
int[] array = {0};
System.out.println(solution(array));
}
static int solution(int[] A) {
int n = A.length;
int result = 0;
for (int i = 0; i < n - 1; i++) {
if (A[i] == A[i + 1])
result = result + 1;
}
int r = -1;
for (int i = 0; i < n; i++) {
int count = 0;
if (i > 0) {
if (A[i - 1] != A[i])
count = count + 1;
else
count = count - 1;
}
if (i < n - 1) {
if (A[i + 1] != A[i])
count = count + 1;
else
count = count - 1;
}
r = Math.max(r, count);
}
return result + r;
}
}