[
array
voting
]
BinarySearch 0393 Majority Vote
Problem statement
https://binarysearch.com/problems/Majority-Vote/
Solution
Equal to Leetcode 0169 Majority Element, but check that frequency is enough.
Complexity
It is O(n)
for time and O(1)
for additional space.
Code
class Solution:
def solve(self, nums):
cnt, cand = 0, None
for num in nums:
if cnt == 0: cand = num
cnt += (1 if num == cand else -1)
return cand if nums.count(cand) > len(nums)//2 else -1