-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path169_majority_element.py
39 lines (32 loc) · 1005 Bytes
/
169_majority_element.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
"""
---
title: Majority Element
number: 169
difficulty: easy
tags: ['Array','Hash Table','Divide and Conquer','Sorting','Counting']
url: https://leetcode.com/problems/majority-element/
solved: true
---
"""
from typing import List
"""
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
"""
class Solution:
def majorityElement(self, nums: List[int]) -> int:
return self.simple(nums)
def simple(self,nums: List[int]):
## Solution with hash map is easy
## Lets try to figure out with O(1) space
# This is some algorithm
res,count = 0,0
for num in nums:
if count == 0:
res = num
count+= (1 if num == res else -1)
return res
if __name__ == '__main__':
nums = [3,2,3]
nums1 = [2,2,1,1,1,2,2]
print(Solution().majorityElement(nums))