Created
January 21, 2022 13:54
-
-
Save mdpabel/e549ebba5819b139c3c29dc04cf0a5d2 to your computer and use it in GitHub Desktop.
This file contains 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 binarySearch(nums, target, first, last): | |
while first <= last: | |
mid = first + (last - first) // 2 | |
if nums[mid] == target: | |
return mid | |
elif nums[mid] > target: | |
last = mid - 1 | |
else: | |
first = mid + 1 | |
return -1 | |
class Solution: | |
def searchRange(self, nums, target): | |
mid = binarySearch(nums, target, 0, len(nums) - 1) | |
res = [-1, -1] | |
left = mid | |
right = mid | |
while left != -1 : | |
res[0] = left | |
left = binarySearch(nums, target, 0, left - 1) | |
while right != -1: | |
res[1] = right | |
right = binarySearch(nums, target, right + 1, len(nums) - 1) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment