Skip to content

completed Binary Search 1 #2317

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

Open
wants to merge 1 commit 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
28 changes: 28 additions & 0 deletions rotated_sorted_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Time Complexity O(logn)
class Solution:
def search(self, nums: List[int], target: int) -> int:
start = 0
end = len(nums)-1
while start <= end:
mid = start + (end-start)//2

if target == nums[mid]:
return mid

# find if element is in which half. One half is guarenteed to be sorted.
if nums[start] <= nums[mid]:
# if left is sorted. Check if element can fall on the left side.
if target < nums[mid] and target >= nums[start]:
end = mid-1
else:
# go to un-sorted side since element can't be found in sorted side.
start = mid+1
else:
# if right is sorted check if element can fall in the right side.
if target > nums[mid] and target<= nums[end]:
start = mid+1
else:
# go to un-sorted side since eleemnt can't be found in sorted side.
end = mid-1
# if not found
return -1
26 changes: 26 additions & 0 deletions search_in_unknown_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
# def get(self, index: int) -> int:

# Time complexity - O(logn)
# Straight forware binary search solution on 10 power 4 sized array.
class Solution:
def search(self, reader: 'ArrayReader', target: int) -> int:
start = 0
end = 10 ** 4
while start <= end:
mid = start + (end - start)//2
arr_val = reader.get(mid)
# end condition 1
if arr_val == target:
return mid
# decision 1 & out of bounds
if arr_val > target:
end = mid -1
else:
# if target is lower than mid
start = mid +1
return -1
31 changes: 31 additions & 0 deletions search_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Time taken to solve the problem = 20 mins
# Time complexity - O(logmn)
# The 2-D array gets filled row wise. If flattended completely. It's a sorted list. Binary search on sorted list is straight forward.
# To get the co-ordinates of the mid element in the 2d array we need to use columns since the array is filled row wise. Dividing by cols gives x cords and mod by cols gives y cords.
# Since X is the nth part and y is the offest in the nth part.
class Solution:
def get_cordinates(self, mid, rows, cols):
co_ordinates_x = mid//cols
co_oridnates_y = mid % cols
return co_ordinates_x, co_oridnates_y

def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
rows = len(matrix)
cols = len(matrix[0])
start = 0
end = (rows*cols) -1
while start <= end:
mid = start + (end-start)//2
x, y = self.get_cordinates(mid, rows, cols)
print(matrix[x][y])
if matrix[x][y] == target:
return True
if matrix[x][y] < target:
start = mid+1
else:
end = mid - 1
return False