cpp-algo-cases/chapter_searching/binary_search_edge.cpp

67 lines
1.8 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: binary_search_edge.cpp
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><E3A3A8><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD> */
int binarySearchInsertion(const vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // <20><>ʼ<EFBFBD><CABC>˫<EFBFBD><CBAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // <20><><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD> m
if (nums[m] < target) {
i = m + 1; // target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [m+1, j] <20><>
} else {
j = m - 1; // <20>׸<EFBFBD>С<EFBFBD><D0A1> target <20><>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [i, m-1] <20><>
}
}
// <20><><EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD> i
return i;
}
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> target */
int binarySearchLeftEdge(vector<int> &nums, int target) {
// <20>ȼ<EFBFBD><C8BC>ڲ<EFBFBD><DAB2><EFBFBD> target <20>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
int i = binarySearchInsertion(nums, target);
// δ<>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1
if (i == nums.size() || nums[i] != target) {
return -1;
}
// <20>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> i
return i;
}
/* <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> target */
int binarySearchRightEdge(vector<int> &nums, int target) {
// ת<><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> target + 1
int i = binarySearchInsertion(nums, target + 1);
// j ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> target <20><>i ָ<><D6B8><EFBFBD>׸<EFBFBD><D7B8><EFBFBD><EFBFBD><EFBFBD> target <20><>Ԫ<EFBFBD><D4AA>
int j = i - 1;
// δ<>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -1
if (j == -1 || nums[j] != target) {
return -1;
}
// <20>ҵ<EFBFBD> target <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> j
return j;
}
/* Driver Code */
int main() {
// <20><><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
vector<int> nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums = ";
printVector(nums);
// <20><><EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ұ߽<D2B1>
for (int target : {6, 7}) {
int index = binarySearchLeftEdge(nums, target);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> " << target << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << index << endl;
index = binarySearchRightEdge(nums, target);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> " << target << " <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ " << index << endl;
}
return 0;
}