forked from Muhammad-Magdi/problem-solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.cpp
73 lines (60 loc) · 1.44 KB
/
BinarySearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
What else?
-BinarySearch in Integer Domain.
-BinarySearch in Real Domain.
Terminology:
Search Space
low
high
median
Check function
You can solve the problem using BinarySearch if and only if:
- You can design a check function whose domain is the problem's search space
and its range is separated into at most one "False" segment and one "True" segment.
Can you design the upper_bound function?
bool isGreater(int a, int b){
return a > b;
}
int upperBound(int *A, int val){
int lo = 0, med, hi = n-1;
while(lo<hi){
mid = (lo+hi)>>1;
if(isGreater(A[med], val)) hi = mid;
else lo = med+1;
}
return lo;
}
We have two cases:
1- Minimization Problems:
-FFFFFFFFFFFFFFFFFFFTTTTTTTTTTTTTTTT
-the range is separated into False-True range.
-the target is the first True
-We ceil the low and floor the median.
bool ok(int val){
//Some Checking Statements
}
int binarySearch(){
int lo = 0, med, hi = 1000000000;
while(lo<hi){
mid = (lo+hi)>>1;
if(ok(A[med])) hi = med;
else lo = med+1;
}
return hi;
}
2- Maximization Problems:
-TTTTTTTTTTTTTTTFFFFFFFFFFFFFFF
-the range is separated into True-False range.
-the target is the last True.
-We ceil the median and floor the high.
bool ok(int val){
//Some Checking Statements
}
int binarySearch(){
int lo = 0, med, hi = 1000000000;
while(lo<hi){
mid = (lo+hi+1)>>1;
if(ok(A[med])) lo = med;
else hi = med-1;
}
return lo;
}