-
Notifications
You must be signed in to change notification settings - Fork 16
/
MajorityElementII.java
42 lines (38 loc) · 1.37 KB
/
MajorityElementII.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
39
40
41
42
// https://leetcode.com/problems/majority-element-ii
// T: O(N)
// S: O(1)
import java.util.ArrayList;
import java.util.List;
public class MajorityElementII {
public List<Integer> majorityElement(int[] nums) {
int candidate1 = 0, candidate2 = 0, frequency1 = 0, frequency2 = 0;
for (int element : nums) {
if (element == candidate1) frequency1++;
else if (element == candidate2) frequency2++;
else if (frequency1 == 0) {
candidate1 = element;
frequency1 = 1;
} else if (frequency2 == 0) {
candidate2 = element;
frequency2 = 1;
} else {
frequency1--;
frequency2--;
}
}
return confirmCandidates(nums, candidate1, candidate2);
}
private List<Integer> confirmCandidates(int[] array, int candidate1, int candidate2) {
final List<Integer> result = new ArrayList<>();
if (frequency(array, candidate1) > array.length / 3) result.add(candidate1);
if (candidate2 != candidate1 && frequency(array, candidate2) > array.length / 3) result.add(candidate2);
return result;
}
private int frequency(int[] array, int val) {
int count = 0;
for (int element : array) {
if (element == val) count++;
}
return count;
}
}