-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathFirstBadVersion.java
More file actions
28 lines (26 loc) · 1 KB
/
FirstBadVersion.java
File metadata and controls
28 lines (26 loc) · 1 KB
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
// PROBLEM LINK : https://leetcode.com/problems/first-bad-version
// This problem extents a VersionControl class defined in the leetcode question.
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int s = 1;
int e = n;
int m = -1;
while(s <= e){
m = s + (e - s)/2;
if(isBadVersion(m)) e = m - 1;
else s = m + 1;
}
return s;
}
}
/*Why this is a binarysearch question ?
* 1 ) This problem has a sorted array.
* 2 ) We should search the first bad product so its a searching question.
*How to solve ?
* 1 ) Implement normal binarysearch algorithm.
* 2 ) Whenever a product is bad, shift to the left ... until the array section wont have
* the bad product, in that case the condition for while loop terminates and start index will
* point at first bad product index in the array.
*/