-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_Majority_elements_2.cpp
64 lines (55 loc) · 1.35 KB
/
LeetCode_Majority_elements_2.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
60
61
62
63
64
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
unordered_map<int, int>m;
for(int i = 0; i < nums.size(); i++) {
m[nums[i]]++;
}
vector<int>ans;
for(auto [i, j] : m) {
if(j > ((nums.size()) / 3)) {
ans.push_back(i);
}
}
return ans;
}
};
Time Complexxity : O(n) n = nums.size();
Space Complexity : O(n);
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int num1 = -1;
int num2 = -1;
int cnt1 = 0;
int cnt2 = 0;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] == num1) cnt1++;
else if(nums[i] == num2) cnt2++;
else if(cnt1 == 0) {
num1 = nums[i];
cnt1++;
}
else if(cnt2 == 0) {
num2 = nums[i];
cnt2++;
}
else{
cnt1--;
cnt2--;
}
}
vector<int>ans;
cnt1 = 0;
cnt2 = 0;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] == num1) cnt1++;
else if(nums[i] == num2) cnt2++;
}
if(cnt1 > (nums.size()) / 3) ans.push_back(num1);
if(cnt2 > (nums.size()) / 3) ans.push_back(num2);
return ans;
}
};
Time Complexity: O(n)
Space Complexity: O(1)