-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathProblem_2.py
More file actions
26 lines (21 loc) · 809 Bytes
/
Copy pathProblem_2.py
File metadata and controls
26 lines (21 loc) · 809 Bytes
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
'''
We start with a search size of 1 and see if the array of search space can contain the target.
If not we move the search window to high and move the high to 2*high meaning increase the search window by
2 times.
Once we get the search window where the target lies, we then do a binary search in the window to get the index.
'''
class Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:
low, high = 0, 1
while reader.get(high) < target:
low = high
high = high * 2
while low <= high:
mid = low + (high - low) // 2
if reader.get(mid) == target:
return mid
if reader.get(mid) > target:
high = mid - 1
else:
low = mid + 1
return -1