Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def searchRange(self, nums: List[int], target: int) -> List[int]:
- def upper_bound(nums: List[int], target:int):
- """Returns the index of last element"""
- low, high = 0, len(nums)-1
- result = -1
- while (low <= high):
- mid = (low + high)//2
- if nums[mid] <= target:
- result = mid
- low = mid + 1
- else:
- high = mid - 1
- if result != -1 and nums[result] != target:
- return -1
- return result
- def lower_bound(nums: List[int], target:int):
- low, high = 0, len(nums)-1
- result = -1
- while (low <= high):
- mid = (low + high)//2
- if nums[mid] >= target:
- result = mid
- high = mid - 1
- else:
- low = mid + 1
- if result != -1 and nums[result] != target:
- return -1
- return result
- start = lower_bound(nums, target)
- end = upper_bound(nums, target)
- return [start, end]
Advertisement
Add Comment
Please, Sign In to add comment