-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
57 lines (42 loc) · 1.2 KB
/
python.py
File metadata and controls
57 lines (42 loc) · 1.2 KB
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
from typing import List
# Solution class containing the majorityElement method
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
count = 1
majority = nums[0]
# Phase 1: Find potential majority candidate
for i in range(1, n):
if nums[i] == majority:
count += 1
else:
count -= 1
if count == 0:
majority = nums[i]
count = 1
# Phase 2: Verify the candidate
count = 0
for i in range(n):
if nums[i] == majority:
count += 1
# Return candidate if it occurs more than n/2 times
if count > n // 2:
return majority
else:
return -1 # No majority element found
# Run the logic directly
if __name__ == "__main__":
# Hardcoded test input
nums = [2, 2, 1, 1, 2, 2, 2]
# Create object and call method
sol = Solution()
result = sol.majorityElement(nums)
# Print result
if result != -1:
print("Majority element is:", result)
else:
print("No majority element found.")