-
Notifications
You must be signed in to change notification settings - Fork 90
/
Majority Number II.py
59 lines (47 loc) · 1.47 KB
/
Majority Number 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
52
53
54
55
56
57
58
59
"""
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.
Find it.
Note
There is only one majority number in the array
Example
For [1, 2, 1, 2, 1, 3, 3] return 1
Challenge
O(n) time and O(1) space
"""
__author__ = 'Danyang'
class Solution:
def majorityNumber(self, nums):
"""
Voting Algorithm
Since majority number must occur more than 1/3, there are at most 2 majority number
reference: http://www.cnblogs.com/yuzhangcmu/p/4175779.html
:param nums:
:return:
"""
n1, n2 = None, None
cnt1, cnt2 = 0, 0
for num in nums:
if num in (n1, n2):
if num == n1:
cnt1 += 1
else:
cnt2 += 1
else: # every time --, discard 3 different numbers
if cnt1 == 0:
n1 = num
cnt1 += 1
elif cnt2 == 0:
n2 = num
cnt2 += 1
else:
cnt1 -= 1
cnt2 -= 1
# double check
if len(filter(lambda x: x == n1, nums)) > len(nums)/3:
return n1
else:
return n2
if __name__ == "__main__":
assert Solution().majorityNumber(
[169, 43, 133, 93, 60, 29, 60, 104, 26, 60, 38, 52, 60, 118, 45, 183, 49, 42, 60, 0, 66, 67, 194, 127, 60, 60,
60, 60, 60, 60]) == 60