Skip to content

Commit c719a82

Browse files
solved on 10/06/24
1 parent 82c69aa commit c719a82

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

CS_56_ImplementUpperBound.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int upperBound(vector<int> &nums, int target, int n)
5+
{
6+
int low = 0, high = n - 1, ans = n;
7+
8+
while (low <= high)
9+
{
10+
int mid = low + (high - low) / 2;
11+
12+
if (nums[mid] > target)
13+
{
14+
ans = mid;
15+
high = mid - 1;
16+
}
17+
else
18+
{
19+
low = mid + 1;
20+
}
21+
}
22+
return ans;
23+
}
24+
25+
int main()
26+
{
27+
vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
28+
int n = nums.size();
29+
int target = 5;
30+
cout << upperBound(nums, target, n) << endl;
31+
return 0;
32+
}

0 commit comments

Comments
 (0)