Skip to content

Commit eacb20a

Browse files
committed
Solve problem: First Bad Version & Binary Search
1 parent aaf526d commit eacb20a

File tree

7 files changed

+132
-25
lines changed

7 files changed

+132
-25
lines changed

README.MD

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,30 @@ Solving problems with LeetCode
44

55
## Problems
66

7-
| Title | Solution | Difficulty |
8-
|--------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|------------|
9-
| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10-
| [Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11-
| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12-
| [Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13-
| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14-
| [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15-
| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16-
| [Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17-
| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18-
| [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19-
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20-
| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21-
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22-
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
23-
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
24-
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
25-
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
26-
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
27-
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
28-
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |
29-
| [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/longestPalindrome/longestPalindrome.js) | Easy |
30-
| [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/preorder/preorder.js) | Easy |
31-
| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/description/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/detectCycle/detectCycle.js) | Medium |
7+
| Title | Solution | Difficulty |
8+
|--------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|------------|
9+
| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/containsDuplicate/index.js) | Easy |
10+
| [Missing Number](https://leetcode.com/problems/missing-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/tree/main/solutions/missingNumber/index.js) | Easy |
11+
| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findDisappearedNumbers/index.js) | Easy |
12+
| [Single Number](https://leetcode.com/problems/single-number) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/singleNumber/index.js) | Easy |
13+
| [Climbing Stairs](https://leetcode.com/problems/climbing-stairs) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/climbStairs/index.js) | Easy |
14+
| [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/maxProfit/index.js) | Easy |
15+
| [Range Sum Query - Immutable](https://leetcode.com/problems/range-sum-query-immutable) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/sumRange/index.js) | Easy |
16+
| [Counting Bits](https://leetcode.com/problems/counting-bits) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/countBits/index.js) | Easy |
17+
| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/hasCycle/index.js) | Easy |
18+
| [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/middleNode/index.js) | Easy |
19+
| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/reverseList/index.js) | Easy |
20+
| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isPalindrome/index.js) | Easy |
21+
| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/removeElements/index.js) | Easy |
22+
| [Two Sum](https://leetcode.com/problems/two-sum) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/twoSum/index.js) | Easy |
23+
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
24+
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
25+
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
26+
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
27+
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
28+
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |
29+
| [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/longestPalindrome/longestPalindrome.js) | Easy |
30+
| [N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/preorder/preorder.js) | Easy |
31+
| [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/findFirstBadVersion/findFirstBadVersion.js) | Easy |
32+
| [Binary Search](https://leetcode.com/problems/binary-search/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/binarySearch/binarySearch.js) | Easy |
33+
| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/description/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/detectCycle/detectCycle.js) | Medium |

solutions/binarySearch/README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [704. Binary Search](https://leetcode.com/problems/binary-search/)
2+
3+
Given an array of integers `nums` which is sorted in ascending order, and an integer `target`, write a function to search `target` in `nums`. If `target` exists, then return its index. Otherwise, return `-1`.
4+
5+
You must write an algorithm with `O(log n)` runtime complexity.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
function binarySearch(nums, target) {
7+
let start = 0,
8+
end = nums.length - 1;
9+
10+
while (start <= end) {
11+
const index = Math.floor((start + end) / 2)
12+
13+
if (nums[index] === target) return index;
14+
if (nums[index] > target) end = index - 1;
15+
if (nums[index] < target) start = index + 1;
16+
}
17+
18+
return -1;
19+
}
20+
21+
module.exports = binarySearch;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const binarySearch = require('./binarySearch');
2+
3+
describe('Binary Search', () => {
4+
it('should return the index of the target element', () => {
5+
expect(binarySearch([1, 3, 5, 7, 9], 3)).toBe(1);
6+
expect(binarySearch([1, 3, 5, 7, 9], 9)).toBe(4);
7+
expect(binarySearch([1, 3, 5, 7, 9], 1)).toBe(0);
8+
expect(binarySearch([1, 3, 5, 7, 9], 5)).toBe(2);
9+
expect(binarySearch([1, 3, 5, 7, 9], 7)).toBe(3);
10+
});
11+
12+
it('should return -1 if the target element is not found', () => {
13+
expect(binarySearch([1, 3, 5, 7, 9], 2)).toBe(-1);
14+
expect(binarySearch([1, 3, 5, 7, 9], 0)).toBe(-1);
15+
expect(binarySearch([1, 3, 5, 7, 9], 10)).toBe(-1);
16+
});
17+
18+
it('should return -1 for an empty array', () => {
19+
expect(binarySearch([], 1)).toBe(-1);
20+
});
21+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [278. First Bad Version](https://leetcode.com/problems/first-bad-version/)
2+
3+
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.
4+
5+
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.
6+
7+
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.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {function} isBadVersion()
3+
* @return {function}
4+
*/
5+
function findFirstBadVersion(isBadVersion) {
6+
/**
7+
* @param {number} n Total versions
8+
* @return {number} The first bad version
9+
*/
10+
return function (n) {
11+
let start = 1,
12+
end = n;
13+
14+
while (end > start) {
15+
const middle = Math.floor((start + end) / 2),
16+
isMiddleBad = isBadVersion(middle);
17+
18+
if (isMiddleBad && !isBadVersion(middle - 1)) return middle;
19+
20+
isMiddleBad
21+
? end = middle
22+
: start = middle + 1;
23+
}
24+
25+
return start;
26+
};
27+
}
28+
29+
module.exports = findFirstBadVersion;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const findFirstBadVersion = require('./findFirstBadVersion');
2+
3+
describe('First Bad Version', () => {
4+
it('should return the first bad version', () => {
5+
const isBadVersion = (version) => version >= 4;
6+
const solution = findFirstBadVersion(isBadVersion)(10);
7+
expect(solution).toEqual(4);
8+
});
9+
10+
it('should return the first bad version when there is only one version', () => {
11+
const isBadVersion = (version) => version === 1;
12+
const solution = findFirstBadVersion(isBadVersion)(1);
13+
expect(solution).toEqual(1);
14+
});
15+
16+
it('should return the first bad version when the bad version is the last one', () => {
17+
const isBadVersion = (version) => version === 10;
18+
const solution = findFirstBadVersion(isBadVersion)(10);
19+
expect(solution).toEqual(10);
20+
});
21+
});
22+

0 commit comments

Comments
 (0)