leetcode-15 bi-weekly game -1287- more than 25% of the number of elements appear in an ordered array

Subject description:

 

 

Method One: dichotomy

class Solution:
    def findSpecialInteger(self, arr: List[int]) -> int:
        span = len(arr)//4 + 1
        for i in range(0,len(arr),span):
            a = bisect.bisect_left(arr,arr[i])
            b = bisect.bisect_right(arr,arr[i])
            if b - a >= span:
                return arr[i]
        return -1

Guess you like

Origin www.cnblogs.com/oldby/p/12084292.html