-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path229.py
37 lines (37 loc) · 1000 Bytes
/
229.py
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
# https://leetcode.com/problems/majority-element-ii/
# Boyer-Moore Voting Algorithm
# TIME: O(N)
# SPACE: O(1)
from typing import List
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
# [1,9,5,6,1,2,0,1]
can1 = can2 = None
cnt1 = cnt2 = 0
for num in nums:
if num == can1:
cnt1 += 1
elif num == can2:
cnt2 += 1
elif cnt1 == 0:
can1 = num
cnt1 = 1
elif cnt2 == 0:
can2 = num
cnt2 = 1
else:
cnt1 -= 1
cnt2 -= 1
res = []
threshold = len(nums) // 3
cnt1 = cnt2 = 0
for num in nums:
if num == can1:
cnt1 += 1
if num == can2:
cnt2 += 1
if cnt1 > threshold:
res.append(can1)
if cnt2 > threshold:
res.append(can2)
return res