-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay71.java
More file actions
52 lines (44 loc) · 1.42 KB
/
Day71.java
File metadata and controls
52 lines (44 loc) · 1.42 KB
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
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
public class Day71 {
public List<Integer> majorityElement(int[] nums) {
List<Integer> result = new ArrayList<>();
int candidate1 = Integer.MIN_VALUE, candidate2 = Integer.MIN_VALUE;
int count1 = 0, count2 = 0;
// Step 1: Find the candidates
for (int num : nums) {
if (num == candidate1)
count1++;
else if (num == candidate2)
count2++;
else if (count1 == 0) {
candidate1 = num;
count1 = 1;
} else if (count2 == 0) {
candidate2 = num;
count2 = 1;
} else {
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for (int num : nums) {
if (num == candidate1)
count1++;
else if (num == candidate2)
count2++;
}
if (count1 > nums.length / 3)
result.add(candidate1);
if (count2 > nums.length / 3)
result.add(candidate2);
return result;
}
public static void main(String[] args) {
MajorityElement solution = new MajorityElement();
int[] nums = {2, 2, 1, 3, 1, 1, 3, 1, 1};
List<Integer> result = solution.majorityElement(nums);
System.out.print(result);
}
}