LeetCode—BinarySearch
date
Apr 3, 2022
Property
slug
LeetCode_BinarySearch
status
Published
tags
LeetCode
summary
Binary Search Summary
type
Post
#1-Find the position of a number in the array:#2-Closest but less than target#3-Closest and bigger that target#4-Same to target and leftest#5-Same to target and rightest#6-Binary Search with STLUsing headerHow to use?
#1-Find the position of a number in the array:
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F43dc7bda-86e1-48d9-8e17-3efe45d6f33f%2Fa2de667c-813d-4bc4-8468-388a7f0ac808%2FUntitled.png%3Ftable%3Dblock%26id%3D69882238-623f-4ac3-9490-74b3f6dee83e%26spaceId%3D43dc7bda-86e1-48d9-8e17-3efe45d6f33f%26expirationTimestamp%3D1738620000000%26signature%3D2Yavs4TCBHU-fW6OliCsCeEgCgGfpCmghKIJABlgMFA?table=block&id=69882238-623f-4ac3-9490-74b3f6dee83e&cache=v2)
// find the position of num in the array;
// return the position(if possible) or -1(if the nums is not exist)
int BinarySearch(vector<int> nums, int target){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + ((right - left) >> 1);
if(nums[mid] == target) return mid;
if(nums[mid] > target){
right = mid - 1;
}else if(nums[mid] < target){
left = mid + 1;
}
}
return -1;
}
#2-Closest but less than target
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F43dc7bda-86e1-48d9-8e17-3efe45d6f33f%2Fc380e32a-dbbb-4e18-9f2b-4c79b74ec7ab%2FUntitled.png%3Ftable%3Dblock%26id%3Dcbc5c011-e0f2-4f93-af8f-274e3a337e07%26spaceId%3D43dc7bda-86e1-48d9-8e17-3efe45d6f33f%26expirationTimestamp%3D1738620000000%26signature%3Do7j9xcsWVJNAjILHiAUNcQW9ZYe3G43KesfKrGida8E?table=block&id=cbc5c011-e0f2-4f93-af8f-274e3a337e07&cache=v2)
// closest but less
int GetMidLeft(vector<int> nums, int target){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + ((right - left) >> 1);
if(nums[mid] >= target){
right = mid - 1;
}else{
left = mid + 1;
}
}
return right;
}
#3-Closest and bigger that target
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F43dc7bda-86e1-48d9-8e17-3efe45d6f33f%2F830e2329-da98-4073-bf46-4cca1e5f5673%2FUntitled.png%3Ftable%3Dblock%26id%3D33f52682-86a0-409e-bfac-b2e162a1f8b6%26spaceId%3D43dc7bda-86e1-48d9-8e17-3efe45d6f33f%26expirationTimestamp%3D1738620000000%26signature%3D6dCcHy-tPuHf6hR7ZSAMm72d9K-vlb5yDG1gVjNGJBk?table=block&id=33f52682-86a0-409e-bfac-b2e162a1f8b6&cache=v2)
// closet and bigger
int GetMidRight(vector<int> nums, int target){
int left = 0, right = nums.size() - 1;
while(left <= right){
int mid = left + ((right - left) >> 1);
if(nums[mid] >= target){
right = mid - 1;
}else{
left = mid + 1;
}
}
return left;
}
#4-Same to target and leftest
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F43dc7bda-86e1-48d9-8e17-3efe45d6f33f%2F8736f64c-d880-4edc-be7f-99e573c49324%2FUntitled.png%3Ftable%3Dblock%26id%3Dc6b81bf5-26d5-4f07-a3f5-5c68bc978448%26spaceId%3D43dc7bda-86e1-48d9-8e17-3efe45d6f33f%26expirationTimestamp%3D1738620000000%26signature%3DEHLz3r6OW3SwwAY3Mx8VEx7WUZialARNQNFhI-kDahw?table=block&id=c6b81bf5-26d5-4f07-a3f5-5c68bc978448&cache=v2)
// same to target and leftest
int LeftBound(vector<int> nums, int target){
if(nums.size() == 0) return -1;
int left = 0, right = nums.size();
while(left < right){
int mid = left + ((right - left) >> 1);
if(nums[mid] == target){
right = mid;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] > target){
right = mid;
}
}
return left;
}
#5-Same to target and rightest
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2F43dc7bda-86e1-48d9-8e17-3efe45d6f33f%2F520d5c6d-e190-412e-8565-28f9d821e723%2FUntitled.png%3Ftable%3Dblock%26id%3Df35e5aa7-c56b-432b-bea0-0ccba9b926fb%26spaceId%3D43dc7bda-86e1-48d9-8e17-3efe45d6f33f%26expirationTimestamp%3D1738620000000%26signature%3DkyrsViowalN_onKU2ieMmBFVWNUX8dIL1HQin8VzHUM?table=block&id=f35e5aa7-c56b-432b-bea0-0ccba9b926fb&cache=v2)
// same to target and rightest
int RightBound(vector<int> nums, int target){
if(nums.size() == 0) return -1;
int left = 0, right = nums.size();
while(left < right){
int mid = left + ((right - left) >> 1);
if(nums[mid] == target){
left = mid + 1;
}else if(nums[mid] < target){
left = mid + 1;
}else if(nums[mid] > target){
right = mid;
}
}
return left - 1;
}
#6-Binary Search with STL
Using header
#include <algorithm>
How to use?
binary_search(arr.begin(), arr.begin()+size, index)
- Function:
- find if an element appears
- Where:
arr
: the finding array;size
: the array range that you want to find;index
: the target index;- Return:
true
/false
(bool);- if index exist in array that return
true
, otherwise returnfalse
;
lower_bound(arr.begin(), arr.begin()+size(), index)
- Function:
- Find if an element appears;
- The
lower_bound()
function performs a dichotomous lookup between the first and last in the first-closed and last-open interval, returning the position of the first element greater than or equal to val (return the address). If all elements are less than val, then the position of last will be returned. - Example:
- example code
int main() {
vector<int> number = {4,10,11,30,69,70,96,100};
auto pos_1 = lower_bound(number.begin(), number.end(), 9);
// get the position == 1
cout << pos_1 - number.begin() << endl;
// get the elem == 10
cout << *pos_1 << endl;
// Larger than all elements, then it will return the last elem == 8
auto pos_1 = lower_bound(number.begin(), number.end(), 111);
}
upper_bound(arr.begin(), arr.begin()+size(), index)
- Function:
- Find the first position greater than a certain element;
- NOT EQUAL!
- Example:
int main() {
vector<int> number = {4,10,11,30,69,70,96,100};
auto pos_1 = upper_bound(number.begin(), number.end(), 10);
// get the position == 2
cout << pos_1 - number.begin() << endl;
// get the elem == 11
cout << *pos_1 << endl;
}