Skip to content

Commit 1509242

Browse files
committed
Adding Folder of Leetcode May Challenge - Problem - 1 - First Bad Version - Accepted Solution
1 parent aafed63 commit 1509242

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
29+
class Solution:
30+
def firstBadVersion(self, n):
31+
first = 0 #Initialize first
32+
while first < n: #Loop till the condition met
33+
middle = (first + n) // 2 #Initialize middle which is int from sum of first + n // 2
34+
if isBadVersion(middle): #Condition-check:If middle is bad version
35+
n = middle #We'll update n to middle int
36+
else: #Condition-check: Else
37+
first = middle + 1 #We'll update first to middle + 1
38+
return first #We'll return first, which will be first bad version
39+

0 commit comments

Comments
 (0)