-
Notifications
You must be signed in to change notification settings - Fork 110
/
229-majority-element-ii.cpp
59 lines (58 loc) · 1.78 KB
/
229-majority-element-ii.cpp
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
53
54
55
56
57
58
59
// Map based solution
// Runs in O(n) time but also takes O(n) space
// Not optimal in terms of space
#include <unordered_map>
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int times = nums.size() / 3;
unordered_map<int, int> counts;
vector<int> result;
for (int i = 0; i < nums.size(); i++) {
if (counts.find(nums[i]) == counts.end()) {
counts[nums[i]] = 1;
} else {
counts[nums[i]]++;
}
}
for (auto it = counts.begin(); it != counts.end(); it++) {
if (it->second > times) {
result.push_back(it->first);
}
}
return result;
}
};
// Using Boyer-Moore Majority Vote algorithm
// Runs in O(n) time, O(1) space
// Optimal, this is what the interviewer wants!!!
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
if (nums.size() == 0) return {};
int cand1 = -1;
int cand2 = -1;
int count1 = 0;
int count2 = 0;
for (int elem : nums) {
if (elem == cand1) {
count1++;
} else if (elem == cand2) {
count2++;
} else if (count1 == 0) {
cand1 = elem;
count1++;
} else if (count2 == 0) {
cand2 = elem;
count2++;
} else {
count1--;
count2--;
}
}
vector<int> result;
if (count(nums.begin(), nums.end(), cand1) > nums.size()/3) result.push_back(cand1);
if (count(nums.begin(), nums.end(), cand2) > nums.size()/3) result.push_back(cand2);
return result;
}
};