Skip to content

Commit 8606f3b

Browse files
committed
leetcode
1 parent 2105c8e commit 8606f3b

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
3+
-* First Bad Version *-
4+
5+
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.
6+
7+
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.
8+
9+
You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
10+
11+
12+
13+
Example 1:
14+
15+
Input: n = 5, bad = 4
16+
Output: 4
17+
Explanation:
18+
call isBadVersion(3) -> false
19+
call isBadVersion(5) -> true
20+
call isBadVersion(4) -> true
21+
Then 4 is the first bad version.
22+
Example 2:
23+
24+
Input: n = 1, bad = 1
25+
Output: 1
26+
27+
28+
Constraints:
29+
30+
1 <= bad <= n <= 231 - 1
31+
32+
*/
33+
34+
bool isBadVersion(int version) {
35+
return false;
36+
}
37+
38+
class Solution {
39+
int firstBadVersion(int n) {
40+
int start = 1;
41+
int end = n;
42+
while (start <= end) {
43+
int mid = end + (end - start) ~/ 2;
44+
if (isBadVersion(mid)) {
45+
end = mid + 1;
46+
} else {
47+
start = mid - 1;
48+
}
49+
}
50+
return start;
51+
}
52+
}

FirstBadVersion/first_bad_version.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
func isBadVersion(version int) bool
4+
5+
func firstBadVersion(n int) int {
6+
var start int = 1
7+
var end int = n
8+
for start <= end {
9+
var mid int = start + (end-start)/2
10+
11+
if isBadVersion(mid) {
12+
end = mid - 1
13+
} else {
14+
start = mid + 1
15+
}
16+
17+
}
18+
return start
19+
20+
}

FirstBadVersion/first_bad_version.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# 🔥 First Bad Version 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Binary Search
4+
5+
```dart
6+
class Solution {
7+
int firstBadVersion(int n) {
8+
int start = 1;
9+
int end = n;
10+
while (start <= end) {
11+
int mid = end + (end - start) ~/ 2;
12+
if (isBadVersion(mid)) {
13+
end = mid + 1;
14+
} else {
15+
start = mid - 1;
16+
}
17+
}
18+
return start;
19+
}
20+
}
21+
```
22+
23+
### Disclaimer:-
24+
25+
This Solution is not available in DART Programing language with is a bummer. Hurts my feeling. But as a man we should implement it no matter what. We are not bunch of wussies who gonna skip it if it's not available in one language we love. Instead we will conquer the sea and rivers and cross the mountains so see what's lies beyond our horizons.
26+
27+
## Solution - 2
28+
29+
```go
30+
func firstBadVersion(n int) int {
31+
var start int = 1
32+
var end int = n
33+
for start <= end {
34+
var mid int = start + (end-start)/2
35+
36+
if isBadVersion(mid) {
37+
end = mid - 1
38+
} else {
39+
start = mid + 1
40+
}
41+
42+
}
43+
return start
44+
45+
}
46+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
115115
- [Shortest Path in a Grid with Obstacles Elimination](ShortestPathInAGridWithObstaclesElimination/shortest_path_in_a_grid_with_obstacles_elimination.dart)
116116
- [Toeplitz Matrix](ToeplitzMatrix/toeplitz_matrix.dart)
117117
- [Missing Number](MissingNumber/missing_number.dart)
118+
- [First Bad Version](FirstBadVersion/first_bad_version.dart)
118119

119120
## Reach me via
120121

0 commit comments

Comments
 (0)