You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#You are a product manager and currently leading a team to develop a new product.
10
+
#Unfortunately, the latest version of your product fails the quality check.
11
+
#Since each version is developed based on the previous version, all the versions after a bad version are also bad.
12
+
#
13
+
#Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one,
14
+
#which causes all the following ones to be bad.
15
+
#
16
+
#You are given an API bool isBadVersion(version) which will return whether version is bad.
17
+
#Implement a function to find the first bad version. You should minimize the number of calls to the API.
18
+
#
19
+
#Example:
20
+
#
21
+
#Given n = 5, and version = 4 is the first bad version.
22
+
#
23
+
#call isBadVersion(3) -> false
24
+
#call isBadVersion(5) -> true
25
+
#call isBadVersion(4) -> true
26
+
#
27
+
#Then 4 is the first bad version.
28
+
classSolution:
29
+
deffirstBadVersion(self, n):
30
+
foriinrange(1, n+1, 1): #Loop through 1 to n + 1 by step 1
31
+
ifisBadVersion(i): #Condition-check: If i is bad version
32
+
returni#We'll return that i which is the first bad version
33
+
34
+
#Explanation:
35
+
#The time complexity will be o(n).
36
+
#For small inputs this will work, but for larger input such as n = 1857686865656 if the bad version is at 185268686565, this will impose time limit error as the algorithm is checking from 1 to 185268686565.
37
+
#Thus in the case binary Search will be optimal to use, and has time complexity is o(log(n)).
0 commit comments