-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path1287.ElementAppearingMoreThan25InSortedArray(binarySearch).py
More file actions
47 lines (40 loc) · 1.3 KB
/
1287.ElementAppearingMoreThan25InSortedArray(binarySearch).py
File metadata and controls
47 lines (40 loc) · 1.3 KB
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
40
41
42
43
44
45
46
47
'''
Given an integer array sorted in non-decreasing order,
there is exactly one integer in the array that occurs
more than 25% of the time.
Return that integer.
Example:
Input: arr = [1,2,2,6,6,6,6,7,10]
Output: 6
Constraints:
- 1 <= arr.length <= 10^4
- 0 <= arr[i] <= 10^5
'''
#Difficulty: Easy
#18 / 18 test cases passed.
#Runtime: 120 ms
#Memory Usage: 15.9 MB
#Runtime: 120 ms, faster than 14.93% of Python3 online submissions for Element Appearing More Than 25% In Sorted Array.
#Memory Usage: 15.9 MB, less than 12.26% of Python3 online submissions for Element Appearing More Than 25% In Sorted Array.
class Solution:
def findSpecialInteger(self, arr: List[int]) -> int:
length = len(arr)
nums = set(arr)
if length == 1:
return arr[0]
for num in nums:
n = self.binarySearch(arr, num, length)
if n:
return n
def binarySearch(self, arr: int, num: int, length: int) -> int:
l = 0
r = length - 1
q = length // 4
while l < r:
m = (l+r) // 2
if arr[m] == num and arr[min(length-1, m+q)] == num:
return num
elif num > arr[m]:
l = m + 1
else:
r = m - 1