Skip to content

Commit 993b820

Browse files
committed
02 (1) find first "..." in sorted array using binary search
1 parent 91c7925 commit 993b820

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### Find Bad Version (find first "..." element in sorted array)
2+
3+
To make life easier, we can consider the very easy situation, only one number exists.
4+
5+
```java
6+
/**
7+
********************************************************************
8+
* case I : left & right right left
9+
| | |
10+
index 0 ===> -1 0
11+
isBad true (null) true
12+
expect = 1 => return left + 1 (return the order not the index of bad version)
13+
********************************************************************
14+
* case II : left & right right left
15+
| | |
16+
index 0 ===> 0 1
17+
isBad false false (null)
18+
expect = 2 => return left + 1 (return the order not the index of bad version)
19+
********************************************************************
20+
* Summary: return left
21+
*/
22+
23+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Time : O(lgN) ; Space: O(1)
3+
* @tag : Binary Search; LintCode Copyright
4+
* @by : Steven Cooks
5+
* @date: Aug 23, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* he code base version is an integer start from 1 to n. One day, someone
10+
* committed a bad version in the code case, so it caused this version and
11+
* the following versions are all failed in the unit tests. Find the first
12+
* bad version.
13+
*
14+
* You can call isBadVersion to help you determine which version is the first
15+
* bad one. The details interface can be found in the code's annotation part.
16+
*
17+
***************************************************************************
18+
* {@link http://www.lintcode.com/en/problem/first-bad-version/ }
19+
*/
20+
package _02_FirstBadVersion;
21+
22+
public class Solution {
23+
24+
public int findFirstBadVersion(int n) {
25+
int left = 0;
26+
int right = n - 1;
27+
while (left <= right) {
28+
int mid = left + (right - left) / 2;
29+
boolean isBad = isBad(mid);
30+
if (!isBad) {
31+
left = mid + 1;
32+
} else {
33+
right = mid - 1;
34+
}
35+
}
36+
// return the order not the index of bad version
37+
// if require return -1 for situation no bad version exists
38+
// then return (left + 1) > n ? -1 : (left + 1);
39+
return left + 1;
40+
}
41+
42+
// wrap the API call
43+
private boolean isBad (int v) {
44+
return VersionControl.isBadVersion(v + 1);
45+
}
46+
47+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package _02_FirstBadVersion;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
public class SolutionTest {
12+
13+
/** Test method for {@link _02_FirstBadVersion.Solution } */
14+
Solution solution;
15+
16+
@Rule
17+
public Timeout globalTimeout = new Timeout(200);
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
solution = new Solution();
22+
}
23+
24+
@After
25+
public void tearDown() throws Exception {
26+
solution = null;
27+
}
28+
29+
@Test
30+
public void Test1() {
31+
int n = 5;
32+
boolean[] controls = new boolean[n];
33+
int expected = 4;
34+
customControls(expected, controls);
35+
VersionControl.setControls(controls);
36+
int actual = solution.findFirstBadVersion(n);
37+
assertEquals(expected, actual);
38+
}
39+
40+
@Test
41+
public void Test2() {
42+
int n = 31;
43+
boolean[] controls = new boolean[n];
44+
int expected = 1;
45+
customControls(expected, controls);
46+
VersionControl.setControls(controls);
47+
int actual = solution.findFirstBadVersion(n);
48+
assertEquals(expected, actual);
49+
}
50+
51+
@Test
52+
public void Test3() {
53+
int n = 4;
54+
boolean[] controls = new boolean[n];
55+
int expected = 4;
56+
customControls(expected, controls);
57+
VersionControl.setControls(controls);
58+
int actual = solution.findFirstBadVersion(n);
59+
assertEquals(expected, actual);
60+
}
61+
62+
@Test
63+
public void Test4() {
64+
int n = 10;
65+
boolean[] controls = new boolean[n];
66+
int expected = 10;
67+
customControls(expected, controls);
68+
VersionControl.setControls(controls);
69+
int actual = solution.findFirstBadVersion(n);
70+
assertEquals(expected, actual);
71+
}
72+
73+
@Test
74+
public void Test5() {
75+
int n = 10;
76+
boolean[] controls = new boolean[n];
77+
int expected = 11;
78+
customControls(expected, controls);
79+
VersionControl.setControls(controls);
80+
int actual = solution.findFirstBadVersion(n);
81+
assertEquals(11, actual);
82+
}
83+
84+
private void customControls(int expected, boolean[] controls) {
85+
for (int i = expected - 1; i < controls.length; i++) {
86+
controls[i] = true;
87+
}
88+
}
89+
90+
91+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package _02_FirstBadVersion;
2+
3+
public class VersionControl {
4+
5+
private static boolean[] versions;
6+
7+
public static boolean isBadVersion(int v) {
8+
return v > 0 && v < versions.length + 1 && versions[v - 1];
9+
}
10+
11+
public static void setControls(boolean[] controls) {
12+
versions = controls;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy