Problem:
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 will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example:
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
-Summary-
Solved by the binary search algorithm
1. First, find a midpoint and start the search
-If mid-point is "False" value, then we can all discard the left of the mid-value, since it should be all "False". (Good version)
-If mid-point is "True" value, then it may or may not be a first bad version fo the code. But we know all right side of the mid-point should be all "True". (Bad version)
2. Search until left and right meet.
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode. Best Time to Buy and Sell Stock [Dynamic Programming] (0) | 2020.06.10 |
---|---|
LeetCode. Climbing Stairs [Dynamic Programming] (0) | 2020.06.09 |
LeetCode. Merge Sorted Array [Sorting and Searching] (0) | 2020.06.08 |
LeetCode. Convert Sorted Array to Binary Search Tree [Trees] (0) | 2020.06.07 |
LeetCode. Binary Tree Level Order Traversal [Trees] (0) | 2020.06.07 |
댓글