Binary Search Variant With Lower Bound (Python)
Find the leftmost position where a sorted list can accept a target using bisect-style binary search lower bound.
Code
def lower_bound(arr, target):
lo, hi = 0, len(arr)
while lo < hi:
mid = (lo + hi) // 2
if arr[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
arr = [1, 2, 2, 3, 4, 6]
assert lower_bound(arr, 2) == 1
assert lower_bound(arr, 5) == 5Report a problem with this exampleOpens a pre-filled issue on GitHub.