Back to course home
0% completed
Vote For New Content
Simple code to use ceiling and floor from prev chapters.
nirmal kumar ravi
Jul 27, 2024
class Solution: def findRange(self, arr, key): # out of bounds check if key < arr[0] or key > arr[-1]: return [-1, -1] def search(go_left): left, right = 0, len(arr) - 1 while left <= right: mid = left + (right - left) // 2 if go_left(key, arr[mid]): right = mid -1 else: left = mid + 1 return right # + 1 because it will point to smaller element to the key floor = search(lambda key, mid: key <= mid) + 1 ceiling = search(lambda key, mid: key < mid) # if key found return [floor, ceiling] if arr[floor] == key else [-1, -1]
0
0
Comments
Comments
On this page