-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMajorityN:3.py
43 lines (37 loc) · 951 Bytes
/
MajorityN:3.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
38
39
40
41
42
43
class Solution:
def majorityElement(self, nums: List[int]) -> List[int]:
if len(nums)==0: return []
if len(nums)==1: return nums
elem=0
cnta=0
cntb=0
a=None
b=None
for n in nums:
if n==a:
cnta+=1
elif n==b:
cntb+=1
elif cnta==0:
a=n
cnta+=1
elif cntb==0:
b=n
cntb+=1
else:
cnta-=1
cntb-=1
#Check if it is majority element
cnta=0
cntb=0
for n in nums:
if n==a:
cnta+=1
if n==b:
cntb+=1
res=[]
if cnta>len(nums)/3:
res.append(a)
if cntb>len(nums)/3 and a!=b:
res.append(b)
return res