-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathEquiLeader.java
More file actions
41 lines (40 loc) · 739 Bytes
/
Copy pathEquiLeader.java
File metadata and controls
41 lines (40 loc) · 739 Bytes
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
39
40
41
public class EquiLeader {
public int solution(int[] A) {
int candidate = 0;
int count = 0;
for (int number : A) {
if (count == 0) {
candidate = number;
count++;
} else if (number == candidate) {
count++;
} else {
count--;
}
}
if (count == 0) {
return 0;
}
int countCandidate = 0;
for (int number : A) {
if (number == candidate) {
countCandidate++;
}
}
if (countCandidate * 2 <= A.length) {
return 0;
}
int result = 0;
int countLeft = 0;
for (int S = 0; S < A.length - 1; S++) {
if (A[S] == candidate) {
countLeft++;
}
if (countLeft * 2 > S + 1
&& (countCandidate - countLeft) * 2 > A.length - 1 - S) {
result++;
}
}
return result;
}
}