Skip to content

Commit 5f5fc8d

Browse files
authored
Create check-if-a-number-is-majority-element-in-a-sorted-array.py
1 parent bdf87f6 commit 5f5fc8d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
import bisect
5+
6+
7+
class Solution(object):
8+
def isMajorityElement(self, nums, target):
9+
"""
10+
:type nums: List[int]
11+
:type target: int
12+
:rtype: bool
13+
"""
14+
if len(nums) % 2:
15+
if nums[len(nums)//2] != target:
16+
return False
17+
else:
18+
if not (nums[len(nums)//2-1] == nums[len(nums)//2] == target):
19+
return False
20+
21+
left = bisect.bisect_left(nums, target)
22+
right= bisect.bisect_right(nums, target)
23+
return right-left > len(nums)//2

0 commit comments

Comments
 (0)