-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathMajority Element II.cpp
More file actions
46 lines (37 loc) · 870 Bytes
/
Majority Element II.cpp
File metadata and controls
46 lines (37 loc) · 870 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
42
43
44
45
46
/*
Majority Element II
===================
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Follow-up: Could you solve the problem in linear time and in O(1) space?
Example 1:
Input: nums = [3,2,3]
Output: [3]
Example 2:
Input: nums = [1]
Output: [1]
Example 3:
Input: nums = [1,2]
Output: [1,2]
Constraints:
1 <= nums.length <= 5 * 104
-109 <= nums[i] <= 109
*/
class Solution
{
public:
vector<int> majorityElement(vector<int> &nums)
{
vector<int> ans;
sort(nums.begin(), nums.end());
int i = 0;
while (i < nums.size())
{
int up = upper_bound(nums.begin(), nums.end(), nums[i]) - nums.begin();
int low = lower_bound(nums.begin(), nums.end(), nums[i]) - nums.begin();
if (up - low > nums.size() / 3)
ans.push_back(nums[i]);
i = up;
}
return ans;
}
};