-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path85.py
More file actions
28 lines (23 loc) · 837 Bytes
/
85.py
File metadata and controls
28 lines (23 loc) · 837 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
# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
def cacheCheck(mid: int, cache: dict) -> bool:
got = cache.get(mid, None)
if got is None:
got = isBadVersion(mid)
cache[mid] = got
return got
def binSearch(left: int, right: int, cache: dict) -> int:
if left <= right:
mid = left + (right - left) // 2
print(f"l: {left}, r: {right}, m: {mid}")
if ((mid == left) or not cacheCheck(mid - 1, cache)) and cacheCheck(mid, cache):
return mid
elif not cacheCheck(mid, cache):
return binSearch(mid + 1, right, cache)
else:
return binSearch(left, mid - 1, cache)
return -1
class Solution:
def firstBadVersion(self, n: int) -> int:
c = dict()
return binSearch(1, n, c)