[
binary search
array
]
Leetcode 1150 Check If a Number Is Majority Element in a Sorted Array
Problem statement
https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/
Solution
Find the first and the last occurences of target
and then calculate how many times we have it.
Complexity
Time complexity is O(log n)
, space is O(1)
.
Code
from bisect import bisect, bisect_left
class Solution:
def isMajorityElement(self, nums, target):
l = bisect_left(nums, target)
r = bisect(nums, target)
return 2*(r - l) > len(nums)