-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_template.py
More file actions
34 lines (31 loc) · 958 Bytes
/
binary_search_template.py
File metadata and controls
34 lines (31 loc) · 958 Bytes
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
"""
1. Use a result variable to hold the best result seen so far
2. Always use l <= r criteria
3. Always move l, r to l, m-1 or m+1, r to avoid getting caught up in loops.
4. If you need min_greater, max_smaller (i,e without the equality), swap the == scenario in the if-else clause.
"""
def min_greater_equal(arr, target):
l, r = 0, len(arr)-1
res = None
while(l <= r):
m = (l + r) // 2
if arr[m] < target:
l = m+1
else:
res = arr[m] # Possible result
r = m-1
return res
def max_smaller_equal(arr, target):
l, r = 0, len(arr)-1
res = None
while(l <= r):
m = (l + r) // 2
if arr[m] <= target:
res = arr[m] # Possible result
l = m+1
else:
r = m-1
return res
if __name__ == "__main__":
#print(min_greater_equal([10, 20, 30, 40, 50, 60], 44))
print(max_smaller_equal([10, 20, 30, 40, 50, 60], 70))