Last active
June 9, 2021 16:40
-
-
Save nhudinhtuan/0c3f14e6b92ef0c8615e73d97baa7a8b to your computer and use it in GitHub Desktop.
Binary search - find the left border
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_left_border(arr, target): | |
left = 0 | |
right = len(arr) - 1 | |
left_border = -1 # keep track of the latest smaller number | |
while left <= right: | |
mid = left + (right - left) // 2 | |
if arr[mid] < target: | |
left_border = mid | |
left = mid + 1 | |
else: # in case arr[mid] >= target | |
right = mid - 1 | |
return left_border |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment