mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-23 12:12:01 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,60 +6,60 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
/* Find an insertion point using binary search (with duplicates) */
|
||||
int binarySearchInsertion(const vector<int> &nums, int target) {
|
||||
int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]
|
||||
int i = 0, j = nums.size() - 1; // Initialize the closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate the midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // target is in the interval [m+1, j]
|
||||
i = m + 1; // Target must be in [m+1, j] if it exists
|
||||
} else {
|
||||
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
|
||||
j = m - 1; // Continue searching [i, m-1] for the insertion point before any equal elements
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for the leftmost target */
|
||||
/* Find the leftmost occurrence of target using binary search */
|
||||
int binarySearchLeftEdge(vector<int> &nums, int target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
// This is equivalent to finding the insertion point of target
|
||||
int i = binarySearchInsertion(nums, target);
|
||||
// Target not found, return -1
|
||||
// If target was not found, return -1
|
||||
if (i == nums.size() || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index i
|
||||
// Target was found; return index i
|
||||
return i;
|
||||
}
|
||||
|
||||
/* Binary search for the rightmost target */
|
||||
/* Find the rightmost occurrence of target using binary search */
|
||||
int binarySearchRightEdge(vector<int> &nums, int target) {
|
||||
// Convert to finding the leftmost target + 1
|
||||
// Reduce the problem to finding the leftmost insertion point of target + 1
|
||||
int i = binarySearchInsertion(nums, target + 1);
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
// j points to the rightmost target, while i points to the first element greater than target
|
||||
int j = i - 1;
|
||||
// Target not found, return -1
|
||||
// If target was not found, return -1
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
// Target was found; return index j
|
||||
return j;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
// Array with duplicate elements
|
||||
// Array containing duplicate elements
|
||||
vector<int> nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
|
||||
cout << "\nArray nums = ";
|
||||
printVector(nums);
|
||||
|
||||
// Binary search left and right boundaries
|
||||
// Find the left and right boundaries using binary search
|
||||
for (int target : {6, 7}) {
|
||||
int index = binarySearchLeftEdge(nums, target);
|
||||
cout << "Index of leftmost element " << target << " is " << index << endl;
|
||||
cout << "Leftmost occurrence of element " << target << " has index " << index << endl;
|
||||
index = binarySearchRightEdge(nums, target);
|
||||
cout << "Index of rightmost element " << target << " is " << index << endl;
|
||||
cout << "Rightmost occurrence of element " << target << " has index " << index << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user