-
Notifications
You must be signed in to change notification settings - Fork 0
/
169.cpp
27 lines (25 loc) · 789 Bytes
/
169.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
// https://leetcode.com/problems/majority-element/?envType=study-plan-v2&envId=top-interview-150
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int majorityElement(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
for (int i = 0; i < n; i++) {
int next = i + (n >> 1);
if (nums[i] == nums[next]) return nums[i];
}
return -1;
}
int optimal(vector<int>& nums) {
// O(N) time as opposed to O(N log N), still only needs O(1) space
int cnt = 0, ans = 0;
for (int x : nums) {
if (cnt == 0) ans = x;
cnt += (ans == x ? 1 : -1);
}
return ans;
// Moore's Voting Algorithm is very interesting
}
};