-
Notifications
You must be signed in to change notification settings - Fork 43
/
first-bad-version.py
158 lines (136 loc) · 3.89 KB
/
first-bad-version.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
278. First Bad Version
Easy
3801
1387
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example 1:
Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
Example 2:
Input: n = 1, bad = 1
Output: 1
Constraints:
1 <= bad <= n <= 231 - 1
"""
# V0
# IDEA : binary search
class Solution(object):
def firstBadVersion(self, n):
# edge case
if n <= 1:
return n
l = 0
r = n
while r >= l:
mid = l + (r - l)//2
if isBadVersion(mid) == True and isBadVersion(mid-1) == False:
return mid
elif isBadVersion(mid) == False:
l = mid + 1
elif isBadVersion(mid) == True:
r = mid - 1
return -1
# V0'
# IDEA : binary search
class Solution(object):
def firstBadVersion(self, n):
left = 1
right = n
while right > left + 1:
mid = (left + right)//2
if SVNRepo.isBadVersion(mid):
end = mid
else:
left = mid
if SVNRepo.isBadVersion(left):
return left
return right
# V1
# https://blog.csdn.net/coder_orz/article/details/52048093
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while True:
mid = (left + right) / 2
if Solution.isBadVersion(mid):
if mid == 1 or not Solution.isBadVersion(mid - 1):
return mid
right = mid - 1
else:
left = mid + 1
# V1'
# https://blog.csdn.net/coder_orz/article/details/52048093
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left < right:
mid = (left + right) / 2
if isBadVersion(mid):
right = mid
else:
left = mid + 1
return left
# V1''
# https://www.jiuzhang.com/solution/first-bad-version/#tag-highlight-lang-python
class Solution:
"""
@param n: An integers.
@return: An integer which is the first bad version.
"""
def findFirstBadVersion(self, n):
start, end = 1, n
while start + 1 < end:
mid = (start + end) // 2
if SVNRepo.isBadVersion(mid):
end = mid
else:
start = mid
if SVNRepo.isBadVersion(start):
return start
return end
# V2
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left < right:
mid = (left + right) / 2
if Solution.isBadVersion(mid):
right = mid
else:
left = mid + 1
return left
# V3
class Solution(object):
def firstBadVersion(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while left <= right:
mid = left + (right - left) / 2
if isBadVersion(mid): # noqa
right = mid - 1
else:
left = mid + 1
return left