-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path229.Majority-Element-II.py
51 lines (44 loc) · 1.11 KB
/
229.Majority-Element-II.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
44
45
46
47
48
49
50
51
# https://leetcode.com/problems/majority-element-ii/
#
# algorithms
# Medium (32.37%)
# Total Accepted: 103,787
# Total Submissions: 320,671
# beats 100.0% of python submissions
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
length = len(nums)
if length == 0:
return []
threshold = length / 3
n1, n2, c1, c2 = nums[0], nums[0], 0, 0
for n in nums:
if n == n1:
c1 += 1
elif n == n2:
c2 += 1
elif c1 == 0:
n1 = n
c1 = 1
elif c2 == 0:
n2 = n
c2 = 1
else:
c1 -= 1
c2 -= 1
c1, c2 = 0, 0
for n in nums:
if n == n1:
c1 += 1
elif n == n2:
c2 += 1
res = []
if c1 > threshold:
res += n1,
if c2 > threshold:
res += n2,
return res