forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority-element.py
More file actions
28 lines (24 loc) · 767 Bytes
/
majority-element.py
File metadata and controls
28 lines (24 loc) · 767 Bytes
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
# Time: O(n)
# Space: O(1)
#
# Given an array of size n, find the majority element.
# The majority element is the element that appears more than [n/2] times.
#
# You may assume that the array is non-empty and the majority element always exist in the array.
#
class Solution:
# @param num, a list of integers
# @return an integer
def majorityElement(self, num):
idx, cnt = 0, 1
for i in xrange(1, len(num)):
if num[idx] == num[i]:
cnt += 1
else:
cnt -= 1
if cnt == 0:
idx = i
cnt = 1
return num[idx]
if __name__ == "__main__":
print Solution().majorityElement([1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6])