Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My solution

Mohammed Dh Abbas

Jun 11, 2024

class Solution: def findRange(self, arr, key): def find_first(): b, e = 0, len(arr) - 1 index = 0 while b <= e: m = (b + e) // 2 if arr[m] >= key: index = m e = m - 1 else: b = m + 1 return index def find_last(): b, e = 0, len(arr) - 1 index = 0 while b <= e: m = (b + e) // 2 if arr[m] <= key: index = m b = m + 1 else: e = m - 1 return index first = find_first() last = find_last() return [first if arr[first] == key else -1, last if arr[last] == key else -1]

0

0

Comments
Comments

On this page