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 another way to go about this

Mohammed Dh Abbas

Jun 12, 2024

import math import sys class Solution: def searchInfiniteSortedArray(self, reader, key): # figure out the reader size by doing binary search between 0 and the # largest integer possible = sys.maxsize def find_array_last_index(): b, e = 0, sys.maxsize index = 0 while b <= e: m = (b + e) // 2 if reader.get(m) == math.inf: e = m - 1 else: b = m + 1 index = m return index # do a binary search to find the first occurrence of the key def search(length): b, e = 0, length index = -1 while b <= e: m = (b + e) // 2 if reader.get(m) >= key: index = m e = m - 1 else: b = m + 1 return index if reader.get(index) == key else -1 length = find_array_last_index() return search(length) v

0

0

Comments
Comments