Skip to content

Commit 590e0e2

Browse files
BarklimBarklim
authored andcommitted
binary search
1 parent c71e97f commit 590e0e2

File tree

8 files changed

+165
-0
lines changed

8 files changed

+165
-0
lines changed

0074-search-a-2d-matrix.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,39 @@ var searchMatrix = function(matrix, target) {
2020
.filter(row => row[0] <= target && row[row.length - 1] >= target)
2121
.find(row => row.includes(target)) !== undefined;
2222
};
23+
24+
// var searchMatrix = function(matrix, target) {
25+
// const ROWS = matrix.length;
26+
// const COLS = matrix[0].length;
27+
28+
// let top = 0;
29+
// let bot = ROWS - 1;
30+
// while (top <= bot) {
31+
// const row = Math.floor((top + bot) / 2);
32+
// if (target > matrix[row][COLS - 1]) {
33+
// top = row + 1;
34+
// } else if (target < matrix[row][0]) {
35+
// bot = row - 1;
36+
// } else {
37+
// break;
38+
// }
39+
// }
40+
41+
// if (!(top <= bot)) {
42+
// return false;
43+
// }
44+
// const row = Math.floor((top + bot) / 2);
45+
// let l = 0;
46+
// let r = COLS - 1;
47+
// while (l <= r) {
48+
// const m = Math.floor((l + r) / 2);
49+
// if (target > matrix[row][m]) {
50+
// l = m + 1;
51+
// } else if (target < matrix[row][m]) {
52+
// r = m - 1;
53+
// } else {
54+
// return true;
55+
// }
56+
// }
57+
// return false;
58+
// };

0704-binary-search.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ var search = function(nums, target) {
2828

2929
return -1;
3030
};
31+
32+
// var search = function(nums, target) {
33+
// l = 0
34+
// r = nums.length
35+
36+
// while (l <= r) {
37+
// pivot = Math.floor((l + r) / 2)
38+
// if (nums[pivot] === target) return pivot
39+
// if (target < nums[pivot]) r = pivot - 1
40+
// else l = pivot + 1
41+
// }
42+
43+
// return - 1
44+
// };

0875-koko-eating-bananas.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,26 @@ var minEatingSpeed = function(piles, h) {
4040

4141
return result;
4242
};
43+
44+
// var minEatingSpeed = function(piles, h) {
45+
// l = 0
46+
// r = Math.max(...piles)
47+
// res = r
48+
49+
// while (l <= r) {
50+
// m = Math.floor((l + r)/2)
51+
52+
// totalTime = 0
53+
// for (const p of piles) {
54+
// totalTime += Math.ceil( p / m)
55+
// }
56+
57+
// if (totalTime <= h) {
58+
// res = m
59+
// r = m - 1
60+
// } else {
61+
// l = m + 1
62+
// }
63+
// }
64+
// return res
65+
// };

example/4.BinarySearch/0074.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[][]} matrix
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
var searchMatrix = function(matrix, target) {
7+
8+
};
9+
10+
const example1 = searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 3); // true
11+
const example2 = searchMatrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 13); // false
12+
const example3 = searchMatrix([[1,2,4,8],[10,11,12,13],[14,20,30,40]], 10); // true
13+
const example4 = searchMatrix([[1,2,4,8],[10,11,12,13],[14,20,30,40]], 15); // false
14+
15+
console.log(example1);
16+
console.log(example2);
17+
console.log(example3);
18+
console.log(example4);

example/4.BinarySearch/0704.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var search = function(nums, target) {
7+
8+
};
9+
10+
const example1 = search([-1,0,3,5,9,12], 9); // 4
11+
const example2 = search([-1,0,3,5,9,12], 2); // -1
12+
const example3 = search([-1,0,2,4,6,8], 4); // 3
13+
const example4 = search([-1,0,2,4,6,8], 3); // -1
14+
15+
console.log(example1);
16+
console.log(example2);
17+
console.log(example3);
18+
console.log(example4);

example/4.BinarySearch/0875.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} piles
3+
* @param {number} h
4+
* @return {number}
5+
*/
6+
var minEatingSpeed = function(piles, h) {
7+
8+
};
9+
10+
const example1 = minEatingSpeed([3,6,7,11], 8); // 4
11+
const example2 = minEatingSpeed([30,11,23,4,20], 5); // 30
12+
const example3 = minEatingSpeed([30,11,23,4,20], 6); // 23
13+
const example4 = minEatingSpeed([1,4,3,2], 9); // 2
14+
const example5 = minEatingSpeed([25,10,23,4], 4); // 25
15+
16+
console.log(example1);
17+
console.log(example2);
18+
console.log(example3);
19+
console.log(example4);
20+
console.log(example5);

example/Dayly/2226.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} candies
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maximumCandies = function(candies, k) {
7+
l = 1
8+
r = Math.max(...candies)
9+
result = 0
10+
11+
while (l <= r) {
12+
mid = Math.floor((l+r)/2)
13+
childredCount = candies.reduce((sum, pile) => sum + Math.floor(pile/mid), 0)
14+
15+
if (childredCount >= k) {
16+
result = mid
17+
l = mid + 1
18+
} else {
19+
r = mid - 1
20+
}
21+
}
22+
23+
return result
24+
};
25+
26+
const example1 = maximumCandies([5,8,6], 3); // 5
27+
const example2 = maximumCandies([2,5], 11); // 0
28+
29+
console.log(example1);
30+
console.log(example2);

example/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Better order to solve problems
6767
- 23. Merge k Sorted Lists
6868
- 25. Reverse Nodes in k-Group
6969

70+
### [Binary Search](https://github.com/Barklim/leetcode-javascript/tree/master/example/4.BinarySearch)
71+
72+
704. Binary Search
73+
74. Search a 2D Matrix
74+
875. Koko Eating Bananas
75+
7076
### HashMap
7177

7278
706. Design Hash map

0 commit comments

Comments
 (0)