Grind75
문제로
Boyer-Moore Voting Algorithm
시간복잡도 $O(1)$
공간복잡도 $O(n)$
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| class Solution {
public int majorityElement(int[] nums) {
int candidate = nums[0];
int count = 0;
for(int num : nums) {
if(count == 0) {
candidate = num;
}
if(candidate == num) {
count++;
} else {
count--;
}
}
return candidate;
}
}
|
