-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path702.py
More file actions
30 lines (27 loc) · 844 Bytes
/
Copy path702.py
File metadata and controls
30 lines (27 loc) · 844 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
27
28
29
# """
# Search in a sorted array of unknown size
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
# def get(self, index: int) -> int:
class Solution:
def findHighIndex(self, reader, target):
high = 1
if reader.get(0) == target:
return 0
while reader.get(high) < target:
high *= 2
return high
def search(self, reader: 'ArrayReader', target: int) -> int:
low = 0
high = self.findHighIndex(reader, target)
while low<=high:
mid = low + (high-low)//2
if reader.get(mid) == target:
return mid
if reader.get(mid) < target:
low = mid + 1
else:
high = mid - 1
return -1