Skip to content

Commit ffa35d3

Browse files
committedMay 1, 2020
Adding BadSolution - Problem - 1 - First Bad Version - Declined Solution
1 parent 1509242 commit ffa35d3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎Leetcode-May-Challenge/BadSolution.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
##==================================
2+
## Leetcode May Challenge
3+
## Username: Vanditg
4+
## Year: 2020
5+
## Problem: 1
6+
## Problem Name: First Bad Version
7+
##===================================
8+
#
9+
#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+
class Solution:
29+
def firstBadVersion(self, n):
30+
for i in range(1, n+1, 1): #Loop through 1 to n + 1 by step 1
31+
if isBadVersion(i): #Condition-check: If i is bad version
32+
return i #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

Comments
 (0)
Please sign in to comment.