You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Changi Cho edited this page Dec 21, 2021
·
5 revisions
lower_bound, upper_bound
lower_bound
찾는 숫자 이상이 처음나오는 index
intlowerBound(vector<int> &nums, int target){
int size = nums.size();
int left = 0, right = size;
while(left < right){
int mid = left + (right - left) / 2;
if(target <= nums[mid]){
// pick left
right = mid;
}elseif(nums[mid] < target){
// pick right
left = mid+1;
}
}
return right;
}
upper_bound
찾는 숫자 초과가 처음 나오는 index
intupperBound(vector<int> &nums, int target){
int size = nums.size();
int left = 0, right = size;
while(left < right){
int mid = left + (right - left) / 2;
if(target < nums[mid]){
// pick left
right = mid;
}elseif (nums[mid] <= target){
// pick right
left = mid + 1;
}
}
return right;
}