Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Binary Search 1 Problems #2173

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Exercise1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#TC: O(log n) - used binary search
#SC: O(1)

class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left = 0
right = len(nums)-1

while left<=right:
mid = (left + right) //2

if nums[mid] == target:
return mid

#Check if left half is sorted
if nums[left] <= nums[mid]:
#target is in left half
if nums[left] <= target < nums[mid]:
right = mid - 1
else: #target in right half
left = mid + 1
#Check sorted right half
else:
#Target in right half
if nums[mid] < target <= nums[right]:
left = mid + 1
#target in left half
else:
right = mid - 1
return -1

40 changes: 40 additions & 0 deletions Exercise2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#TC: O(log m x n) = O(log n)
#m: rows
#n: columns
#SC: O(1)
class Solution(object):
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
rows, cols = len(matrix), len(matrix[0])
top, bot = 0, rows-1

while(top<= bot):
row = (top+bot)//2
if(target > matrix[row][-1]):
top = row+1
elif(target < matrix[row][0]):
bot = row - 1
else:
break
if not (top<= bot):
return False

row = (top+bot)//2
print(row)
l, r = 0, cols-1

while(l <= r):
mid = (l+r)//2
print(mid)
if(target > matrix[row][mid]):
l = mid+1
elif(target < matrix[row][mid]):
r = mid-1
else:
return True

return False
46 changes: 46 additions & 0 deletions Exercise3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader(object):
# def get(self, index):
# """
# :type index: int
# :rtype int
# """


#TC: O(log n) + O(log n) = O(log n)
# Finding the upper bound will take O(log n) because the search range grows exponentially and then we have to perform a binary search which also takes O(log n)
#SC: O(1)

class Solution(object):
def search(self, reader, target):
"""
:type reader: ArrayReader
:type target: int
:rtype: int
"""
low = 0
high = 1

#Find upper bound
while reader.get(high) < target:
low = high
high *= 2 #Exponentially expand the search range

#Perform the binary search
while low<= high:
mid = (low+high)//2
val = reader.get(mid)

if val == target:
return mid
#If mid is out of bounds, shrink the search space
elif val > target or val == 2**31-1:
high = mid - 1
else:
low = mid + 1
return -1